2

In Rust Polars, how to cast a Series or ChunkedArray to a Vec?

kmdreko
  • 42,554
  • 6
  • 57
  • 106
Hakase
  • 211
  • 1
  • 12

1 Answers1

4

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(())
}
ritchie46
  • 10,405
  • 1
  • 24
  • 43
  • How can I do this for str type? I am getting error if I do this ...```let asvec: Vec<_> = self.se.str()?.into_iter().collect(); | ^^^^^^ method not found in `polars::prelude::Series```` – librasteve Jun 08 '22 at 14:12
  • 1
    OK I found [here](https://pola-rs.github.io/polars/polars/docs/eager/index.html) that for str values, replace ```i32()``` with ```utf8()```! – librasteve Jun 09 '22 at 11:01
  • 1
    @ritchie46 is there a way to do this without knowing the type of the series? – ste_kwr Jun 10 '22 at 20:58