pandas/apply-comparison
| Pack | Severity | Fix | Versions |
|---|---|---|---|
| pandas | warning | unsafe, with xpd check --fix --unsafe-fixes |
All versions |
A comparison inside apply runs Python once for every value. Compare the whole column instead.
high = df["load"].apply(lambda value: value > 50)high = df["load"] > 50What the rule reports
Section titled “What the rule reports”The rule reports df["a"].apply(lambda v: v > 50) and df["a"].apply(lambda v: v in wanted), which compare one value at a time. It leaves a chain such as 0 < v < 5 alone, and a test on text such as "ta" in v or v in "abc".
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["a"] > 50, df["a"].isin(wanted).
The isin fix is unsafe, because a dict, a range or a custom container can answer in differently from isin.
Custom Python objects can also compare differently when pandas compares the whole column.
Diagnostic message
Section titled “Diagnostic message”A comparison per value runs in Python. Write $NEW on the whole column.See all rules in the pandas pack.