pandas/groupby-apply-reads-key
| Pack | Severity | Fix | Versions |
|---|---|---|---|
| pandas | error | none, edit by hand | All versions |
groupby.apply no longer passes the grouping column to the function, so reading it inside the function raises KeyError. Read the group key from the group's name instead.
labels = df.groupby("region").apply(lambda group: group["region"].iloc[0] + ": " + ", ".join(group["site"]))labels = df.groupby("region").apply(lambda group: group.name + ": " + ", ".join(group["site"]))What the rule reports
Section titled “What the rule reports”The rule reports df.groupby("k").apply(lambda g: g["k"]), which raises KeyError.
How to fix it
Section titled “How to fix it”xpd has no automatic fix, so make the change by hand.
group.name exists only inside the function passed to apply, and with more than one group key it holds a tuple.
Diagnostic message
Section titled “Diagnostic message”groupby.apply no longer passes the grouping column $KEY to the function, so this lookup raises KeyError. Read the group key from $G.name instead.See all rules in the pandas pack.