0

I am a little confused. Creating a polars series with a vector of "simple" options

Vec<Option<f64>>

works ... but a vector of options with complex values (e.g. vectors / arrays) does not?!

Vec<Option<Vec<f64>>>

Why? ... and how to make it work?

use polars::prelude::*;

fn main() {

    // This does NOT work
    let v = vec![Some(vec![0.123, 67.12]), None, Some(vec![2.123, 44.98])];

    // This does NOT work
    let v = vec![Some([0.123, 67.12]), None, Some([2.123, 44.98])];

    // This does work
    let v = vec![Some(0.123), None, Some(44.98)];

    let s = Series::new("got nulls", v);

    println!("{:?}", s)
}

This is the error message I get when I try to make let v = vec![Some(vec![0.123, 67.12]), None, Some(vec![2.123, 44.98])]; into a Series:

error[E0277]: the trait bound `polars::prelude::Series: polars::prelude::NamedFrom<Vec<Option<Vec<{float}>>>, _>` is not satisfied
  --> src\main.rs:13:38
   |
13 |     let s = Series::new("got nulls", v);
   |             -----------              ^ the trait `polars::prelude::NamedFrom<Vec<Option<Vec<{float}>>>, _>` is not implemented for `polars::prelude::Series`
   |             |
   |             required by a bound introduced by this call
   |
   = help: the following other types implement trait `polars::prelude::NamedFrom<T, Phantom>`:
             <polars::prelude::Series as polars::prelude::NamedFrom<&polars::prelude::Series, str>>
             <polars::prelude::Series as polars::prelude::NamedFrom<T, ListType>>
             <polars::prelude::Series as polars::prelude::NamedFrom<T, T>>
             <polars::prelude::Series as polars::prelude::NamedFrom<T, [&'a str]>>
             <polars::prelude::Series as polars::prelude::NamedFrom<T, [AnyValue<'a>]>>
             <polars::prelude::Series as polars::prelude::NamedFrom<T, [Cow<'a, str>]>>
             <polars::prelude::Series as polars::prelude::NamedFrom<T, [NaiveDateTime]>>
             <polars::prelude::Series as polars::prelude::NamedFrom<T, [NaiveDate]>>
           and 37 others
Robert
  • 131
  • 1
  • 7
  • "doesn't work" is not a enough description of a problem. – Stargateur Sep 02 '22 at 05:35
  • Thanks @Stargateur ... I will be more precise next time. I have edited the question to better show the error I get. – Robert Sep 02 '22 at 06:06
  • Not sure what you are aiming, maybe you need this implementation: https://docs.rs/polars/latest/polars/prelude/struct.Series.html#impl-NamedFrom%3CT%2C%20%5BOption%3CSeries%3E%5D%3E-for-Series – Ömer Erden Sep 02 '22 at 06:14

1 Answers1

0

Here is a workaround inspired by this:

Create a struct -> Json -> DataFrame -> Series.

Its a little convoluted, but works for me.

Any suggestions for a better solution are welcome :)

use polars::prelude::*;
use std::io::Cursor;

fn main() {
    #[derive(serde::Serialize)]
    struct ListData {
        data: Option<Vec<f64>>,
    }

    impl ListData {
        fn new(value: Option<Vec<f64>>) -> Self {
            ListData { data: value }
        }
    }

    let test_vec: Vec<ListData> = vec![
        ListData::new(Some(vec![0.123, 67.12])),
        ListData::new(None),
        ListData::new(Some(vec![2.123, 44.98])),
    ];

    let json = serde_json::to_string(&test_vec).unwrap();

    let cursor = Cursor::new(json);

    let df = JsonReader::new(cursor).finish().ok().unwrap();

    let s = df.column("data").unwrap().clone();

}

Robert
  • 131
  • 1
  • 7