pandas/zip-over-columns
| Rule | zip-over-columns |
|---|---|
| Pack | pandas |
| Severity | warning |
| Fix | none, edit by hand |
| Library versions | every version |
A loop over zipped columns runs Python once for every row. Compute on whole columns instead.
out = [load * weight for load, weight in zip(df["load"], df["weight"])]out = (df["load"] * df["weight"]).tolist()What the rule reports
Section titled “What the rule reports”The rule reports a loop or comprehension over zip(df["a"], df["w"]), which works on one pair of values at a time. It reports only when every argument of zip is a column taken by a text label, or a .values, .to_numpy() or .tolist(). It leaves dict(zip(...)) alone, because it isn't a loop. The rule doesn't run on test files.
How to fix it
Section titled “How to fix it”xpd has no automatic fix, so make the change by hand.
Keep the result as a Series when the next step takes one, and call tolist only where the next step needs a list.
Message
Section titled “Message”xpd prints this message with the issue. A name that starts with $ stands for code from your file.
A loop over zipped columns runs Python once per row. Compute on whole columns instead, such as df["a"] * df["w"].Browse the other rules of the pandas pack.