Cast an object into a list in Polars

63 Views Asked by At

I have two Polars Series that contain lists as objects, something like:

series1 = pl.Series(["[1.2, 3.4]", "[5.6, 7.8]", "[9.1, 2.3]"])
series2 = pl.Series(["[1.0, 3.0]", "[5.0, 7.0]", "[9.0, 2.0]"])

I need to operate with both of them, so I'm doing:

def parse_list(string_rep):
    return ast.literal_eval(string_rep)

parsed_series1 = series1.map_elements(parse_list)
parsed_series2 = series2.map_elements(parse_list)

However, my original series are very big, and the operation above is taking around 30min to run. In my case, it isn't acceptable.

I was thinking in to cast the Series instead, something like:

series1.cast(pl.List)

but I'm getting a Series where all the elements are null:

enter image description here

Any help is highly appreciated.

Polars version: 0.20.9

1

There are 1 best solutions below

0
jqurious On BEST ANSWER

As mentioned, in this case the strings you're working are valid JSON.

Meaning you can use .str.json_decode()

series1.str.json_decode()
shape: (3,)
Series: '' [list[f64]]
[
    [1.2, 3.4]
    [5.6, 7.8]
    [9.1, 2.3]
]