Search code examples
pythonstructpython-polars

How to convert Struct to Series in Polars?


I have Dataframe with stuct inside after used pl.cum_fold(). How struct convert to normal series based column?

┌───────────────────────────────────┐
│ price                             │
│ ---                               │
│ struct[2000]                      │
╞═══════════════════════════════════╡
│ {null,null,null,null,30302.67187… │
└───────────────────────────────────┘

Is it possible with polars expression or some kind of util method? I find the only way is convert to Python and then back to DF.

df = pl.DataFrame(pl.Series('price', df[0, 0].values()))

Solution

  • There is transpose once the struct is unnested. This will be relatively slow but I don't think there is any other way:

    df = pl.DataFrame( {"price": {'test0' : None, 'test1' : 1, 'test2' : 2}})
    df.unnest('price').transpose(column_names=['price'])
    
    shape: (3, 1)
    ┌───────┐
    │ price │
    │ ---   │
    │ f64   │
    ╞═══════╡
    │ null  │
    │ 1.0   │
    │ 2.0   │
    └───────┘