rustapache-arrowrust-polars

How to get a polars Series from an arrow_rs FixedSizeList?


Suppose I'm using another library whose output is an arrow_rs FixedSizeList, how do I get that into a polars Series?

For simplicity, here's a function that can serve as another library source.

use arrow::array::FixedSizeListArray;
use arrow::array::types::Int32Type;


fn fixed_size_list_source()->FixedSizeListArray {
    let data = vec![
       Some(vec![Some(0), Some(1), Some(2)]),
       None,
       Some(vec![Some(3), None, Some(5)]),
       Some(vec![Some(6), Some(7), Some(45)]),
    ];
    FixedSizeListArray::from_iter_primitive::<Int32Type, _, _>(data, 3)
}

I've tried using from_arrow_rs but I can't make a &dyn arrow::array::Array which it expects as an input

I've tried

let list_array = fixed_size_list_source();
let la= Box::new(list_array).into();

but I get

the trait bound `&dyn arrow::array::Array: From<Box<arrow::array::FixedSizeListArray>>` is not satisfied
required for `Box<arrow::array::FixedSizeListArray>` to implement `Into<&dyn arrow::array::Array>`

Solution

  • FixedSizeList implements arrow::array::Array, and that can be converted into Box<dyn polars_arrow::array::Array>, which in turn can be converted into Series:

    fn fixed_size_list_into_series(value: &FixedSizeListArray) -> Series {
        Series::try_from((
            "Series name",
            Box::<dyn polars_arrow::array::Array>::from(value as &dyn arrow::array::Array),
        ))
        .unwrap()
    }
    

    You will need to have polars, arrow, and polars-arrow with the arrow_rs feature enabled.