pandas/subtract-shift
| Pack | Severity | Fix | Versions |
|---|---|---|---|
| pandas | warning | unsafe, with xpd check --fix --unsafe-fixes |
All versions |
Subtracting a column's own shift() computes the difference from the previous row by hand. Call diff() instead.
change = df["load"] - df["load"].shift()change = df["load"].diff()What the rule reports
Section titled “What the rule reports”The rule reports df["b"] - df["b"].shift(). The fix is unsafe, because diff wraps around on a column of unsigned integers and returns booleans for a boolean column.
The rule leaves a comparison or other boolean expression alone when it can identify one in the code.
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["b"].diff().
Diagnostic message
Section titled “Diagnostic message”diff() is the difference from the previous row. Write $S.diff().See all rules in the pandas pack.