Skip to content

pandas/reduction-level-argument

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

The level argument of reductions such as sum was removed, so the call raises TypeError on pandas 3. Group by the level instead, as in df.groupby(level=0).sum().

totals = df["load"].sum(level="region")
totals = df["load"].groupby(level="region").sum()

The rule reports df.sum(level=0) and the other reductions with level=, which raise TypeError. The fix applies when level= is the only argument. It is unsafe, because xpd could not run the old form on pandas 3 to compare the two results.

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 df.groupby(level=0).sum().

The level argument of $R was removed and raises TypeError on pandas 3. Write $X.groupby(level=$L).$R().

See all rules in the pandas pack.