2

Looking for help populating and using structs within a polars Dataframe/Series.

I know there is a JSON workaround to get structs into the df... but there should be a more straight forward solution rather than this "awkward" workaround?!

Once the structs made it into a df ... how do I get them out?

Here is some code I use to better understand how polars handles various data types:

use polars::prelude::*;

enum FriendType {
    Goodfriend,
    Badfriend,
}
struct MyStruct {
    pub person: Option<String>,
    pub relation: FriendType,
}

impl MyStruct {
    fn new(person: &str, relation: FriendType) -> Self {
        MyStruct {
            person: Some(person.to_string()),
            relation,
        }
    }
}

fn main() {
    // HOW to get the MyStruct_col into a polars series/dataframe
    let df = df![
        "Integer_col" => [1,2,3,4],
        "Float_col" => [1.1,0.3,9.6,4.2],
        "String_col" => ["apple", "pear", "lemon", "plum"],
        "values_nulls" => [Some(1), None, Some(3), Some(8)],
        // "MyStruct_col" => [MyStruct::new("Peter",Badfriend),
        //     MyStruct::new("Mary",Goodfriend),
        //     MyStruct::new("Elon",Goodfriend),
        //     MyStruct::new("Joe",Badfriend),
        // ],
    ]
    .unwrap();

    // HOW do I get them out again?
    for each_column_name in df.get_column_names() {
        let df_col = df.column(each_column_name).unwrap();

        println!("\nFound TYPE: {}", df_col.dtype());

        /*----------------------------INTEGER_COLUMN----------------------------------------------*/
        if df_col.dtype() == &DataType::Int32 {
            let df_col_chunked = df_col.i32().unwrap();

            for each_element in df_col_chunked {
                match each_element {
                    Some(_) => each_element,
                    None => continue,
                };

                let single_element = each_element.unwrap();

                println!(
                    "Element: {:?} as String: {}",
                    single_element,
                    single_element.to_string()
                )
            }
        }
        /*----------------------------FLOAT64_COLUMN----------------------------------------------*/
        if df_col.dtype() == &DataType::Float64 {
            let df_col_chunked = df_col.f64().unwrap();

            for each_element in df_col_chunked {
                match each_element {
                    Some(_) => each_element,
                    None => continue,
                };

                let single_element = each_element.unwrap();

                println!(
                    "Element: {} as String: {}",
                    single_element,
                    single_element.to_string()
                )
            }
        };
        /*----------------------------STRING_COLUMN----------------------------------------------*/
        if df_col.dtype() == &DataType::Utf8 {
            let df_col_chunked = df_col.utf8().unwrap();
            for each_element in df_col_chunked {
                match each_element {
                    Some(_) => each_element,
                    None => continue,
                };

                let single_element = each_element.unwrap();

                println!(
                    "Element: {} as String: {}",
                    single_element,
                    single_element.to_string()
                )
            }
        };
        /*----------------------------STRUCT_COLUMN----------------------------------------------*/
        // if (df_col.dtype() == &MyStruct) {
        //     let df_col_chunked = df_col.struct_().unwrap();
        //     for each_element in df_col_chunked {
        //         match each_element {
        //             Some(_) => each_element,
        //             None => continue,
        //         };
        //
        //         let single_element = each_element.unwrap();
        //
        //         println!(
        //             "Element: {} as String: {}",
        //             single_element,
        //             single_element.to_string()
        //         )
        //     }
        // };
    }
}

Robert
  • 131
  • 1
  • 7
  • Custom datatypes are not supported as far as I can tell. https://docs.rs/polars/latest/polars/datatypes/ – fvg Oct 28 '22 at 19:21

0 Answers0