Skip to content

pandas/rename-after-agg

Rule rename-after-agg
Pack pandas
Severity warning
Fix none, edit by hand
Library versions every version

Renaming columns right after agg names them in a second step. Name them in agg itself, as in agg(b_mean=("b", "mean")).

summary = df.groupby("region").agg({"load": "mean"}).rename(columns={"load": "mean_load"})
summary = df.groupby("region").agg(mean_load=("load", "mean"))

The rule reports g.agg({"b": "mean"}).rename(columns={"b": "b_mean"}).

xpd has no automatic fix, so make the change by hand.

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

Name the result columns in agg itself, as in agg(b_mean=("b", "mean")), instead of renaming them afterwards.

Browse the other rules of the pandas pack.