Skip to content

pandas/empty-guard

Pack Severity Fix Versions
pandas warning unsafe, with xpd check --fix --unsafe-fixes All versions

The mean, median and similar reductions of an empty Series are already NaN, with no error. Drop the test for an empty frame.

share = (capped["upper"] < LOW_PRICE).mean() if len(capped) else float("nan")
share = (capped["upper"] < LOW_PRICE).mean()

The rule reports x if len(frame) else nan where x is mean, median, std, var, min, max or quantile of a column picked with a string key. It leaves sum alone, because the sum of an empty Series is 0, and it leaves a NumPy array alone, because np.mean of an empty array warns.

xpd check --fix --unsafe-fixes applies the fixes of this rule. Check the result, because an unsafe fix can change what the code does.

The unsafe fix writes the reduction without the test, because the frame in the test and the frame in the reduction can differ.

xpd applies the replacement with --fix --unsafe-fixes.

With warnings turned into errors, mean, median, std, var, min, max and quantile(0.5) of pd.Series([], dtype=float) each returned nan on pandas 3.1.1. The test repeats what pandas does, and a reader has to check that both branches give the same answer.

$VALUE is nan for an empty Series already, with no error and no warning. Drop the test for an empty frame.

See all rules in the pandas pack.