Skip to content

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

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.

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

diff() is the difference from the previous row. Write $S.diff().

See all rules in the pandas pack.