pandas/subtract-shift
| Rule | subtract-shift |
|---|---|
| Pack | pandas |
| Severity | warning |
| Fix | unsafe, with xpd check --fix --unsafe-fixes |
| Library versions | every version |
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.
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().
Message
Section titled “Message”xpd prints this message with the issue. A name that starts with $ stands for code from your file.
diff() is the difference from the previous row. Write $S.diff().Browse the other rules of the pandas pack.