Pandas Time Series Shift & Lag Examples

Updated: February 19, 2024 By: Guest Contributor Post a comment


In this tutorial, we will dive deep into working with time series data in Pandas, focusing on shifting and lagging techniques. These techniques are fundamental when dealing with time series analysis, as they allow us to compare data over time, perform time-based calculations, and model time series forecasting. We’ll start with the basics and gradually move towards more advanced examples. You’ll learn how to effectively use these methods for your time series data analysis projects.

Understanding Pandas Shift & Lag

The shift() function in Pandas is primarily used to shift the index by the desired number of periods, with an optional time frequency. This is especially useful in time series forecasting, where you want to compare observations to previous time steps (lag) or future time steps (lead).

Meanwhile, the concept of ‘lagging’ data involves creating a copy of a time series dataset where the observations are shifted so that each record is associated with the time before it. This is useful for creating features that can help model trends or seasonal patterns in your data.

Setting Up Your Environment

Before we delve into examples, ensure you have Pandas installed in your environment:

pip install pandas

Also, make sure to import Pandas in your script:

import pandas as pd

Basic Examples

Let’s begin with some basic examples to understand the shift and lag functionality.

Creating a Simple Time Series Data Frame

dates = pd.date_range('20230101', periods=6)
data = {'value': [1, 3, 5, 7, 9, 11]}
df = pd.DataFrame(data, index=dates)

This will create a DataFrame with dates as the index and a simple sequence of numbers as the values.

Shifting Data

df_shifted = df.shift(1)

In the output, you’ll notice that all the values have been shifted down by one period, introducing NaN values for the first period.

Advanced Examples

As we progress, let’s explore more complex scenarios and learn how to leverage the shift and lag functions for in-depth analysis.

Shifting Based on Frequency

When working with time series data, sometimes you need to shift your data by a specific time frequency, such as shifting all your data points one month into the future. Pandas allows for this with the freq parameter.

df_shifted = df.shift(periods=1, freq='M')

Creating Lag Features for Machine Learning

One powerful application of shifting is creating lag features for machine learning models. The idea is to use previous observations to predict future values. For instance, using sales data from previous months to predict future sales.

df['lag_1'] = df['value'].shift(1)
df['lag_2'] = df['value'].shift(2)

This adds two new columns to your DataFrame, each as a lagged version of the original ‘value’ column, useful for predictive modeling.

Handling Missing Values

When shifting data, you will often encounter NaN values for the periods that do not have a corresponding value due to the shift. It’s important to decide how to handle these. Common strategies include filling them with a fixed value, forward-filling with the last valid observation, or backfilling with the next valid observation.

df_filled = df_shifted.fillna(method='ffill')


Through this tutorial, we’ve explored how to effectively use the Pandas shift() and lag() functions to manipulate time series data for various analytical purposes. By understanding these techniques, you can uncover insights into your time series datasets, create features for predictive modeling, and perform sophisticated time-based comparisons. With practice, these tools become an essential part of your data manipulation toolkit.