Reputation: 1008
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,
Upvotes: 0
Views: 996
Reputation: 1918
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"] }
Upvotes: 1