pandas/index-plus-offset
| Pack | Severity | Fix | Versions |
|---|---|---|---|
| pandas | warning | unsafe, with xpd check --fix --unsafe-fixes |
All versions |
Adding an offset to the index by hand replaces the index in a separate step. Call shift(freq=), which moves the index and leaves the values alone.
ts.index = ts.index + pd.Timedelta(hours=6)ts = ts.shift(freq=pd.Timedelta(hours=6))What the rule reports
Section titled “What the rule reports”The rule reports ts.index = ts.index + pd.Timedelta(hours=6) and ts.index += timedelta(days=1), where shift(freq=) moves the index 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 ts = ts.shift(freq=pd.Timedelta(hours=6)).
The fix is unsafe, because the assignment changes the object in place and shift returns a new one.
Diagnostic message
Section titled “Diagnostic message”shift(freq=) moves the index by an offset and leaves the values alone. Write $X = $X.shift(freq=$D).See all rules in the pandas pack.