Perform Rolling Computations on Time Series Data Using Pandas Rolling() Function
What is the rolling() function in Pandas?
The rolling()
function in Pandas is a powerful tool for performing rolling computations on time series data. Essentially, the rolling()
function splits the data into a “window” of size n
, computes some function on that window (for example, the mean) and then moves the window over to the next n
observations and repeats the process. This can be incredibly useful for identifying patterns and trends in time series data.
To use rolling()
, you first need to create a Pandas DataFrame or Series object containing your time series data. From there, you can apply the rolling()
function to your data by chaining it to your DataFrame or Series object. Here’s an example using the mean function:
import pandas as pd
# create some dummy time series data
data = pd.Series([1, 2, 3, 4, 5])
# apply the rolling() function to compute the rolling mean with a window size of 3
rolling_mean = data.rolling(window=3).mean()
print(rolling_mean)
This will output the following:
0 NaN
1 NaN
2 2.000000
3 3.000000
4 4.000000
dtype: float64
As you can see, the rolling()
function has computed the rolling mean for each window of size 3 in the data. Note that the first two values are NaN
since there aren’t enough observations to compute a mean.
The rolling()
function can be customized in a number of different ways. You can adjust the size of the window (window
parameter), the function that is computed on the window (min
, max
, sum
, count
, etc.), and the way in which the window is aligned with the observations (center
, right
, or left
). For example:
# create some dummy time series data
data = pd.Series([1, 3, 4, 7, 11, 10, 12, 15])
# compute the rolling sum with a window of size 4 and align the window to the right
rolling_sum = data.rolling(window=4, min_periods=1, center=False).sum()
print(rolling_sum)
This will output the following:
0 1.0
1 4.0
2 8.0
3 15.0
4 25.0
5 32.0
6 37.0
7 49.0
dtype: float64
As you can see, we’ve customized the rolling()
function to compute the rolling sum with a window of size 4 and aligned the window to the right. The min_periods=1
parameter tells Pandas to compute the function even if there aren’t enough observations to fill the window. This is why the first three values of rolling_sum
are equal to the respective values in data
.
How to use rolling() for rolling window calculations
Using the rolling()
function in Pandas is straightforward once you’ve prepared your data. There are a few key parameters you can tweak to customize the behavior of the function according to your needs.
The first parameter to consider is the window
parameter, which sets the size of the moving window. This parameter specifies the number of consecutive observations that will be used to compute the function. A larger window size will result in a smoother output, but it will also introduce more lag in the results. Conversely, a smaller window size will result in more variability in the output, but it will be more sensitive to shortterm changes in the data.
Another key parameter is the min_periods
parameter, which specifies the minimum number of observations required to calculate the rolling statistic. By default, min_periods
is set to the size of the window, but you can customize this to fit your needs. For example, if you want to calculate the rolling mean of the last 3 observations, but you only have 2 observations in your dataset, setting min_periods=1
will allow you to calculate the rolling mean using those 2 observations.
The center
parameter controls the alignment of the rolling window with respect to the index of the data. By default, center
is set to False
, which means that the window is aligned to the right of the current observation. Setting center
to True
will align the window to the center of the observations.
Let’s see an example of how to use these parameters to calculate the rolling mean of a time series:
import pandas as pd
import numpy as np
# create a datetime index
idx = pd.date_range('20220101 09:00:00', periods=10, freq='T')
# create random time series data
data = pd.Series(np.random.randint(0, 100, size=(len(idx))))
# resampling to 5minute intervals
data = data.resample('5T').ohlc()
# calculate rolling mean with a window size of 2
rolling_mean = data['close'].rolling(window=2, min_periods=1, center=False).mean()
# print the result
print(rolling_mean)
This will output:
20220101 09:00:00 71.0
20220101 09:05:00 17.0
20220101 09:10:00 25.0
20220101 09:15:00 10.0
Here we’re using the rolling()
function to calculate the rolling mean of the close
column of our DataFrame. We’ve set the window size to 2, so the function is computing the mean of each pair of consecutive observations. We’ve also set min_periods
to 1, which means that even if there’s only one observation left in the window, we’ll still calculate the mean. Lastly, we’ve set center
to False
, which means that the window is aligned to the right of each observation.
Tips for optimizing your rolling computations
Here are some tips for optimizing your rolling computations:

Use vectorized operations: One way to speed up your rolling computations is to use vectorized operations. Vectorized operations can be applied to entire arrays at once, which can be faster than looping through each observation individually. For example, instead of using a for loop to calculate the rolling mean of a time series, you can use the
rolling()
function to create a DataFrame with rolling statistics for each observation, and then apply a vectorized operation to that DataFrame. 
Avoid using loops: Loops can be slow, especially when you’re working with large datasets. If possible, try to avoid using loops to perform your rolling computations. Instead, use builtin Pandas functions and methods that are optimized for speed and efficiency.

Use a rolling window on normalized data: Normalizing your data can help to make your rolling computations more accurate and efficient. If your data has a large range of values, applying a rolling window directly to the raw data can result in large variations in the rolling statistics. By normalizing your data before applying the rolling window, you can reduce these variations and get more accurate results.

Use the
rolling()
function properly: Therolling()
function has a number of parameters that can affect the speed and accuracy of your rolling computations. Be sure to use these parameters properly to optimize your code. For example, setting themin_periods
parameter to a low value can help to speed up your code, but it can also affect the accuracy of your results if you have missing data. 
Use a moving average instead of a rolling window: In some cases, a moving average may be more appropriate than a rolling window for calculating statistics on time series data. A moving average weights each observation in the time series equally, whereas a rolling window gives more weight to observations closer to the current time. Depending on your specific use case, a moving average may be more accurate or more efficient than a rolling window.
By following these tips, you can optimize your rolling computations and get more accurate and efficient results.
Summary
In this article, we’ve discussed the rolling()
function in Pandas for performing rolling computations on time series data. We’ve explored some key parameters you can customize to get the results you need, and we’ve shared some tips on optimizing your rolling computations. If you work with time series data, the rolling()
function is a powerful tool to have in your toolkit. By understanding how to use it effectively, you can gain valuable insights into trends and patterns in your data that can help you make more informed decisions. My personal advice is to experiment with different window sizes and functions to find the right balance between smoothness and sensitivity in your results. And remember to pay attention to the parameters you use, as they can have a significant impact on the accuracy and efficiency of your code.
Related Posts

The Ultimate Python Pandas Guide
By: Adam RichardsonIn this ultimate guide, you will learn how to use Pandas to perform various data manipulation tasks, such as cleaning, filtering, sorting and aggregating data.

A StepbyStep Guide to Joining Pandas DataFrames
By: Adam RichardsonLearn how to join pandas DataFrames efficiently with this stepbystep guide. Improve your data analysis skills and optimize your workflow today!

Appending DataFrames in Pandas: A Tutorial
By: Adam RichardsonLearn how to combine two DataFrames in Pandas using the Append function. This tutorial will guide you on how to join multiple DataFrames with code examples.

Calculating Mean Value Using mean() Function in Pandas
By: Adam RichardsonLearn how to use the mean() function in pandas to calculate the mean value of a dataset in Python. Improve your data analysis skills with this tutorial.