Polars convert string of digits to list

Question:

So i have a polars column/series that is strings of digits.

s = pl.Series("a", ["111","123","101"])
s
shape: (3,)
Series: 'a' [str]
[
    "111"
    "123"
    "101"
]

I would like to convert each string into a list of integers.
I have found a working solution but i am not sure if it is optimal.

s.str.split("").arr.shift(1).arr.slice(2).arr.eval(pl.element().str.parse_int(10))
shape: (3,)
Series: 'a' [list[i32]]
[
    [1, 1, 1]
    [1, 2, 3]
    [1, 0, 1]
]

I first split the strings at each point. For the first row this gives me ["","1","1","1",""]. From this i want to remove the first and last entries (the empty string). Since i dont know the length of the entries beforehand and slice doesnt let one specify an end index i went with the shift -> slice version but i feel that there has to be a better way.

Lastly is the application of the parse_int.

This seems to be working but id like to know if there are better ways to do this or any of the individual steps.

Asked By: J.N.

||

Answers:

.extract_all() and .cast()

s.str.extract_all(r"d").cast(pl.List(pl.Int64))
shape: (3,)
Series: 'a' [list[i64]]
[
    [1, 1, 1]
    [1, 2, 3]
    [1, 0, 1]
]
Answered By: jqurious
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.