Pandas DataFrame | tshift method
Start your free 7-days trial now!
Pandas DataFrame.tshift(~) method shifts the date index of the source DataFrame.
tshift(~) is now deprecated - use shift(~) instead.
Parameters
1. periodslink | int | optional
The amount to shift by. If axis=0 (the default), then positive integers mean rows are shifted downwards, while negative integers mean rows are shifted upwards. The same logic applies for when axis=1 (column shifts).
By default, periods=1.
2. freqlink | DateOffset or timedelta or string | optional
The time unit to shift by. By default, freq=None.
3. axis | int or string | optional
Whether to shift the row index or the column index:
Axis | Description |
|---|---|
| Shift row index. |
| Shift column index. |
By default, axis=0.
Return Value
A new DataFrame with a shifted index.
Examples
Consider the following DataFrame:
date_index = pd.date_range("2020/12/25", periods=4)df = pd.DataFrame({"A":[2,3,4,5],"B":[6,7,8,9]}, index=date_index)df
A B2020-12-25 2 62020-12-26 3 72020-12-27 4 82020-12-28 5 9
Here, the index of df is of type DatetimeIndex.
Periods parameter
To shift the row index by 1 day:
df.tshift()
A B2020-12-26 2 62020-12-27 3 72020-12-28 4 82020-12-29 5 9
To shift the row index by 2 days:
df.tshift(periods=2)
A B2020-12-27 2 62020-12-28 3 72020-12-29 4 82020-12-30 5 9
To back-shift the row index by 1 day, set a negative period like so:
df.tshift(periods=-1)
A B2020-12-24 2 62020-12-25 3 72020-12-26 4 82020-12-27 5 9
Frequency parameter
Consider the same DataFrame as above:
df
A B2020-12-25 2 62020-12-26 3 72020-12-27 4 82020-12-28 5 9
By default, freq is inferred from the date index. Since our df has a freq of a day, that is, each date index varies by a single day, tshift will use freq="D":
df.tshift(periods=1) # freq="D"
A B2020-12-26 2 62020-12-27 3 72020-12-28 4 82020-12-29 5 9
Here, each date index is shifted by 1 day unit. To shift by 3 day units, for instance:
df.tshift(periods=1, freq="3D")
A B2020-12-28 2 62020-12-29 3 72020-12-30 4 82020-12-31 5 9
Note that this essentially has the same effect as specifying periods=3:
df.tshift(periods=3)
A B2020-12-28 2 62020-12-29 3 72020-12-30 4 82020-12-31 5 9
Warning
Consider the following DataFrame:
date_index = pd.date_range("2020/12/30", periods=4)df = pd.DataFrame({"A":[2,3,4,5],"B":[6,7,8,9]}, index=date_index)df
A B2020-12-30 2 62020-12-31 3 72021-01-01 4 82021-01-02 5 9
Setting freq="M" yields:
df.tshift(periods=1, freq="M")
A B2020-12-31 2 62021-01-31 3 72021-01-31 4 82021-01-31 5 9
The results is slightly unintuitive since we'd expect an one-month offset to be applied to each date index.
Note the following:
each date index got shifted to the end of the month.
since we specified
freq="M", the day unit of the date index becomes irrelevant.the exception is when the day unit is at the end of the month (e.g.
12-31), in which case the month will get shifted (e.g. to01-31).