Search code examples
dataframeruststructrust-polars

Creating Polars Dataframe from Vec<Struct>


Supposing I have a vector of structs like so:


struct Test {
    id:u32,
    amount:u32
 }
 
 fn main() {
    let test_vec:Vec<Test> = vec![Test{id:1,amount:3}, Test{id:3,amount:4}];
 }

Is there a way to get this into a polars dataframe with the column names being the struct fields?

Hoping to get an output as follows:

   id  amount
0   1       3
1   3       4

Solution

  • After a lot of head banging, I found the following solution.

    If you have a vector of a custom struct, to get it into a Polars dataframe you can do the following:

    // 1. Derive serde::Serialize for your struct
    
    #[derive(Serialize)]
    struct Test {
        id:u32,
        amount:u32
    }
    
    // (Adding new method here for quality of life).
    
    impl Test {
        fn new(id:u32, amount:u32) -> Self{
            Test{id,amount}
        }
    }
    
    
    // 2. Jsonify your struct Vec
    let test_vec:Vec<Test> = vec![Test::new(1,3), Test::new(3,4)];
    let json = serde_json::to_string(&test_vec).unwrap();
    
    // 3. Create cursor from json 
    let cursor = Cursor::new(json);
    
    // 4. Create polars DataFrame from reading cursor as json
    let df = JsonReader::new(cursor)
                .finish()
                .unwrap();