Skip to content

pandas/index-plus-offset

Rule index-plus-offset
Pack pandas
Severity warning
Fix unsafe, with xpd check --fix --unsafe-fixes
Library versions every version

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

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.

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.

xpd prints this message with the issue. A name that starts with $ stands for code from your file.

shift(freq=) moves the index by an offset and leaves the values alone. Write $X = $X.shift(freq=$D).

Browse the other rules of the pandas pack.