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()What the rule reports
Section titled “What the rule reports”| 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 |
How to fix it
Section titled “How to fix it”Use xpd check --fix --unsafe-fixes to rewrite supported temporal and categorical casts. Edit other forms using their replacement examples.
Message
Section titled “Message”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.