pandas/divide-by-shift
| Rule | divide-by-shift |
|---|---|
| Pack | pandas |
| Severity | warning |
| Fix | unsafe, with xpd check --fix --unsafe-fixes |
| Library versions | every version |
Dividing a column by its own shift computes the change from the earlier row by hand. Call pct_change() instead.
change = df["load"] / df["load"].shift(96) - 1change = df["load"].pct_change(96)What the rule reports
Section titled “What the rule reports”The rule reports df["load"] / df["load"].shift(96) - 1 and (df["load"] - df["load"].shift(96)) / df["load"].shift(96), which pct_change computes in one call.
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["load"].pct_change(96).
The fix is unsafe, because (s - s.shift(n)) / s.shift(n) rounds differently. The result is close to the old one and not equal bit for bit. On pandas 2 pct_change also filled missing values forward first.
Message
Section titled “Message”xpd prints this message with the issue. A name that starts with $ stands for code from your file.
pct_change() is the change from the earlier row as a share of it. Write $S.pct_change().Browse the other rules of the pandas pack.