Skip to content

pandas/assign-echo-keywords

Pack Severity Fix Versions
pandas warning none, edit by hand All versions

Computing local variables only to pass them straight into .assign(a=a, b=b, c=c) duplicates every column name across both the assignment lines and the keyword list. Inline each expression directly in .assign(...).

def add_metrics(frame: pd.DataFrame) -> pd.DataFrame:
net = frame["imports"] - frame["exports"]
total = frame["imports"] + frame["exports"]
ratio = frame["imports"] / frame["exports"]
return frame.assign(net=net, total=total, ratio=ratio)
return frame.assign(
net=frame["imports"] - frame["exports"],
total=frame["imports"] + frame["exports"],
ratio=frame["imports"] / frame["exports"],
)

The rule reports a .assign(...) call that passes three or more name=name keyword arguments whose keyword name and local variable identifier are identical.

The rule leaves alone .assign(...) calls with fewer than three echoed keywords, calls that pass inline expressions, and calls that unpack a computed mapping.

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

Inline each expression directly inside .assign(...) and delete the temporary local variable.

This assign call passes three or more local variables as name=name keywords. Inline each expression.

See all rules in the pandas pack.