search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

Pandas DataFrame | tshift method

schedule Aug 11, 2023
Last updated
local_offer
PythonPandas
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

Pandas DataFrame.tshift(~) method shifts the date index of the source DataFrame.

WARNING

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

0 or "index"

Shift row index.

1 or "columns"

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 B
2020-12-25 2 6
2020-12-26 3 7
2020-12-27 4 8
2020-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 B
2020-12-26 2 6
2020-12-27 3 7
2020-12-28 4 8
2020-12-29 5 9

To shift the row index by 2 days:

df.tshift(periods=2)
A B
2020-12-27 2 6
2020-12-28 3 7
2020-12-29 4 8
2020-12-30 5 9

To back-shift the row index by 1 day, set a negative period like so:

df.tshift(periods=-1)
A B
2020-12-24 2 6
2020-12-25 3 7
2020-12-26 4 8
2020-12-27 5 9

Frequency parameter

Consider the same DataFrame as above:

df
A B
2020-12-25 2 6
2020-12-26 3 7
2020-12-27 4 8
2020-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 B
2020-12-26 2 6
2020-12-27 3 7
2020-12-28 4 8
2020-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 B
2020-12-28 2 6
2020-12-29 3 7
2020-12-30 4 8
2020-12-31 5 9

Note that this essentially has the same effect as specifying periods=3:

df.tshift(periods=3)
A B
2020-12-28 2 6
2020-12-29 3 7
2020-12-30 4 8
2020-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 B
2020-12-30 2 6
2020-12-31 3 7
2021-01-01 4 8
2021-01-02 5 9

Setting freq="M" yields:

df.tshift(periods=1, freq="M")
A B
2020-12-31 2 6
2021-01-31 3 7
2021-01-31 4 8
2021-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. to 01-31).

robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!