pandas/groupby-then-reset-index
| Pack | Severity | Fix | Versions |
|---|---|---|---|
| pandas | warning | unsafe, with xpd check --fix --unsafe-fixes |
All versions |
reset_index() after a groupby moves the keys back into columns in a second step. Pass as_index=False to groupby instead.
totals = df.groupby("region")["load"].sum().reset_index()totals = df.groupby("region", as_index=False)["load"].sum()What the rule reports
Section titled “What the rule reports”The rule reports df.groupby("k")[["a", "b"]].sum().reset_index(). The fix is unsafe, because a key that is not a column comes back in another place.
How to fix it
Section titled “How to fix it”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("k", as_index=False)[["a", "b"]].sum().
Diagnostic message
Section titled “Diagnostic message”Pass as_index=False to groupby instead of resetting the index afterwards.See all rules in the pandas pack.