Skip to content

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()

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.

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().

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.