Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cast string column in Polars to numeric coercing errors

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 image 314
Camilo Piñón Avatar asked Oct 22 '25 21:10

Camilo Piñón


1 Answers

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     │
└───────────────┴────────┘
like image 169
ignoring_gravity Avatar answered Oct 25 '25 10:10

ignoring_gravity