Skip to content

pandas/apply-comparison

Rule apply-comparison
Pack pandas
Severity warning
Fix unsafe, with xpd check --fix --unsafe-fixes
Library versions every version

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"] > 50

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".

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.

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

A comparison per value runs in Python. Write $NEW on the whole column.

Browse the other rules of the pandas pack.