Skip to content

polars/polars-removed-cast

Rule polars-removed-cast
Pack Polars
Severity error
Fix unsafe, with xpd check --fix --unsafe-fixes
Library versions Polars 2.0 and later

Polars 2.0 removed several casts that need an explicit conversion. Parse temporal strings with the string namespace and construct nested values explicitly.

pl.Series(["2025-01-01"]).cast(pl.Date)
pl.Series(["2025-01-01"]).str.to_date()
Form Replacement example Proof on 2.1.0
pl.Series(["2025-01-01"]).cast(pl.Date) pl.Series(["2025-01-01"]).str.to_date() InvalidOperationError
pl.Series([1]).cast(pl.Categorical) pl.Series([1]).cast(pl.String).cast(pl.Categorical) ComputeError
pl.Series([1]).cast(pl.List(pl.Int64)) pl.Series([[1]], dtype=pl.List(pl.Int64)) InvalidOperationError
pl.Series(["a"]).cast(pl.Categorical).cast(pl.Int64) pl.Series(["a"]).cast(pl.Categorical).to_physical() ComputeError

Use xpd check --fix --unsafe-fixes to rewrite supported temporal and categorical casts. Edit other forms using their replacement examples.

xpd prints this message with the issue. A name that starts with $ stands for code from your file.

Polars 2.1.0 raises InvalidOperationError for this cast. Parse strings with str.to_date.

Browse the other rules of the Polars pack.