I'm working with Polars (version 0.19.0) in Python and facing an issue with converting a string column to numeric while coercing errors. My goal is to convert strings to integers, where non-numeric strings are replaced with nulls.
Here's an example of my DataFrame:
import polars as pl
df = pl.DataFrame({
"str_column": ["3", "4", "unavailable", "random_String", "17"]
})
I want to convert the str_column to integers, expecting the following output: | str_column | |------------| | 3 | | 4 | | null | | null | | 17 |
I tried using to_integer(strict=False) like this:
df.with_columns(pl.col("str_column").str.parse_int(strict=False))
However, this converts everything to null instead of the expected output.
How can I achieve the desired conversion in Polars (preferably using native polars operations)?
like this
In [4]: import polars as pl
...:
...: df = pl.DataFrame({
...: "str_column": ["3", "4", "unavailable", "random_String", "17"]
...: })
In [5]: df.with_columns(parsed=pl.col('str_column').cast(pl.Int64, strict=False))
Out[5]:
shape: (5, 2)
┌───────────────┬────────┐
│ str_column ┆ parsed │
│ --- ┆ --- │
│ str ┆ i64 │
╞═══════════════╪════════╡
│ 3 ┆ 3 │
│ 4 ┆ 4 │
│ unavailable ┆ null │
│ random_String ┆ null │
│ 17 ┆ 17 │
└───────────────┴────────┘
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With