Reputation: 321
In Rust Polars, how to cast a Series
or ChunkedArray
to a Vec
?
Upvotes: 5
Views: 5097
Reputation: 24582
If your series contains PolarsNumericType
values, you can use to_vec
or to_vec_null_aware
depending on the null
values.
use polars::prelude::*;
fn main(){
let series = Series::new("col", &[Some(1), Some(2), Some(3)]);
if series.null_count() == 0 {
println!("{:?}", series.i32().unwrap().to_vec_null_aware().left().unwrap())
} else {
println!("{:?}", series.i32().unwrap().to_vec());
}
}
to_vec()
will convert the series
into Vec
of Option<i32>
. But to_vec_null_aware
will convert series
into Vec<i32>
if there are no null values.
Upvotes: 1
Reputation: 14670
You can collect
the values into a Vec
.
use polars::prelude::*;
fn main() -> Result<()> {
let s = Series::new("a", 0..10i32);
let as_vec: Vec<Option<i32>> = s.i32()?.into_iter().collect();
// if we are certain we don't have missing values
let as_vec: Vec<i32> = s.i32()?.into_no_null_iter().collect();
Ok(())
}
Upvotes: 7