Skip to content

polars/polars-when-without-otherwise

Rule polars-when-without-otherwise
Pack Polars
Severity info
Fix unsafe, with xpd check --fix --unsafe-fixes
Library versions every version

Polars returns null for unmatched rows when then has no otherwise. Supply otherwise if those rows need a value.

df.select(pl.when(expr > 1).then(expr))
df.select(pl.when(expr > 1).then(expr).otherwise(0))

The unhandled row held None. The explicit branch returned 0.

With --unsafe-fixes, xpd adds .otherwise(None) to make the existing null result explicit. Supply a different value if unmatched rows need one.

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

Polars returns null for unmatched rows when then has no otherwise. Supply otherwise if those rows need a value.

Browse the other rules of the Polars pack.