0

What's the alternative of pandas :

data['ColumnA'].str[:2]

in rust polars? My first guess was:

let x = Utf8Chunked::new("ColumnA", &["Pear", "apple", "toly", "x"]);
let y = x.slice(0, 2);

I'd like to get an array/ChunkedArray/Utf8Chunked which looks something like ["Pe", "ap", "to", "x"]. But .slice() actually just takes first n elements of an array. Is it possible without iterating over the array? (as my understanding iterating would slow down the performance). Many thanks,

Anatoly Bugakov
  • 772
  • 1
  • 7
  • 18

1 Answers1

1

You want to use the str_slice method on ChunkedArray:

let x = Utf8Chunked::new("ColumnA", &["Pear", "apple", "toly", "x"]);
dbg!(x.str_slice(0, Some(2)));

Note that you must enable the strings feature of polars in your Cargo.toml:

[dependencies]
polars = { version = "0.21.1", features = ["strings"] }
Niklas Mohrin
  • 1,756
  • 7
  • 23