Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Write a program in Python to shift a dataframe index by two periods in positive and negative direction
In pandas, you can shift DataFrame values by a specified number of periods using the shift() method. This is useful for time series analysis, creating lag variables, or comparing data across different time periods.
Understanding DataFrame Shifting
The shift() method moves data along the specified axis:
Positive values shift data down (forward in time)
Negative values shift data up (backward in time)
Shifted positions are filled with
NaNvalues
Syntax
DataFrame.shift(periods=1, freq=None, axis=0, fill_value=None)
Creating the DataFrame
Let's create a time-indexed DataFrame to demonstrate shifting ?
import pandas as pd
# Create time series with 12-hour frequency
time_series = pd.date_range('01-01-2020', periods=5, freq='12H')
df = pd.DataFrame({"Id": [1, 2, 3, 4, 5],
"Age": [10, 12, 14, 11, 13]},
index=time_series)
print("Original DataFrame:")
print(df)
Original DataFrame:
Id Age
2020-01-01 00:00:00 1 10
2020-01-01 12:00:00 2 12
2020-01-02 00:00:00 3 14
2020-01-02 12:00:00 4 11
2020-01-03 00:00:00 5 13
Shifting by Two Periods in Positive Direction
Shifting by +2 periods moves data down by 2 positions ?
import pandas as pd
time_series = pd.date_range('01-01-2020', periods=5, freq='12H')
df = pd.DataFrame({"Id": [1, 2, 3, 4, 5],
"Age": [10, 12, 14, 11, 13]},
index=time_series)
print("Shift by 2 periods in positive direction:")
shifted_positive = df.shift(2, axis=0)
print(shifted_positive)
Shift by 2 periods in positive direction:
Id Age
2020-01-01 00:00:00 NaN NaN
2020-01-01 12:00:00 NaN NaN
2020-01-02 00:00:00 1.0 10.0
2020-01-02 12:00:00 2.0 12.0
2020-01-03 00:00:00 3.0 14.0
Shifting by Two Periods in Negative Direction
Shifting by −2 periods moves data up by 2 positions ?
import pandas as pd
time_series = pd.date_range('01-01-2020', periods=5, freq='12H')
df = pd.DataFrame({"Id": [1, 2, 3, 4, 5],
"Age": [10, 12, 14, 11, 13]},
index=time_series)
print("Shift by 2 periods in negative direction:")
shifted_negative = df.shift(-2, axis=0)
print(shifted_negative)
Shift by 2 periods in negative direction:
Id Age
2020-01-01 00:00:00 3.0 14.0
2020-01-01 12:00:00 4.0 11.0
2020-01-02 00:00:00 5.0 13.0
2020-01-02 12:00:00 NaN NaN
2020-01-03 00:00:00 NaN NaN
Complete Example
Here's a comprehensive example showing both positive and negative shifts ?
import pandas as pd
# Create time-indexed DataFrame
time_series = pd.date_range('01-01-2020', periods=5, freq='12H')
df = pd.DataFrame({"Id": [1, 2, 3, 4, 5],
"Age": [10, 12, 14, 11, 13]},
index=time_series)
print("Original DataFrame:")
print(df)
print("\nShift by +2 periods (positive direction):")
print(df.shift(2, axis=0))
print("\nShift by -2 periods (negative direction):")
print(df.shift(-2, axis=0))
Original DataFrame:
Id Age
2020-01-01 00:00:00 1 10
2020-01-01 12:00:00 2 12
2020-01-02 00:00:00 3 14
2020-01-02 12:00:00 4 11
2020-01-03 00:00:00 5 13
Shift by +2 periods (positive direction):
Id Age
2020-01-01 00:00:00 NaN NaN
2020-01-01 12:00:00 NaN NaN
2020-01-02 00:00:00 1.0 10.0
2020-01-02 12:00:00 2.0 12.0
2020-01-03 00:00:00 3.0 14.0
Shift by -2 periods (negative direction):
Id Age
2020-01-01 00:00:00 3.0 14.0
2020-01-01 12:00:00 4.0 11.0
2020-01-02 00:00:00 5.0 13.0
2020-01-02 12:00:00 NaN NaN
2020-01-03 00:00:00 NaN NaN
Key Points
| Direction | Parameter | Effect | NaN Position |
|---|---|---|---|
| Positive | shift(2) |
Move data down | Top rows |
| Negative | shift(-2) |
Move data up | Bottom rows |
Conclusion
The shift() method is essential for time series analysis. Use positive values to shift data forward and negative values to shift backward, with NaN filling the empty positions.
