How to Use Rolling Statistics for Time Series Analysis in Python

How to Use Rolling Statistics for Time Series Analysis in Python
Image by Author | Piktochart

Rolling statistics are measurements, typically statistical (such as means, medians, cumulative sums, or standard deviations), dynamically calculated over a sliding window within a dataset. They are instrumental in time series data.

For instance, if we have daily temperature recordings for a year, we can calculate a 10-day rolling average temperature to gain a broader perspective on how temperatures vary and evolve across seasons. In other words, rolling statistics are a smooth and reliable tool for analyzing trends in sequential data, such as time series, enabling the detection of patterns like seasonality and the effective identification of anomalies.

This article shows how to calculate rolling statistics on time series data in Python.

Using Rolling Statistics in Python

The Pandas library provides the necessary classes and methods for calculating rolling statistics, such as:

  • DataFrame and Series: classes to contain two-dimensional and one-dimensional datasets
  • The rolling() method used to create a sliding window
  • Methods to calculate common statistics, like mean(), median(), std(), min(), max()

Time to see how to put these elements together through a practical example:

1. Load the Chicago Bus-Train rides dataset, concretely a version available here.

import pandas as pd

url = "https://raw.githubusercontent.com/gakudo-ai/open-datasets/refs/heads/main/CTA_-_Ridership_-_Daily_Boarding_Totals.csv"
df = pd.read_csv(url)

We can optionally set the date-time column as our default time series index, as follows:

df['service_date'] = pd.to_datetime(df['service_date'])
df.set_index('service_date', inplace=True)

2. Calculate the rolling mean of daily bus rides (bus column), for a 7-day window and a 30-day window. Calculating the same statistic for windows of different sizes in a common approach to analyze short-term and long-term trends.

bus_ridership = df['bus']
df['Rolling_Mean_7'] = bus_ridership.rolling(window=7).mean()
df['Rolling_Mean_30'] = bus_ridership.rolling(window=30).mean()

Notice how the rolling() and mean() methods are jointly used to calculate our rolling statistics, both of which are stored as new columns in the dataset.

3. Calculate other rolling statistics and display them all.

df['Rolling_Std_7'] = bus_ridership.rolling(window=7).std() 
df['Rolling_Sum_7'] = bus_ridership.rolling(window=7).sum()
df['Rolling_Median_7'] = bus_ridership.rolling(window=7).median()

print(df[['bus', 'Rolling_Mean_7', 'Rolling_Mean_30', 'Rolling_Std_7', 'Rolling_Sum_7', 'Rolling_Median_7']].tail())

Output:

   bus  Rolling_Mean_7  Rolling_Mean_30  Rolling_Std_7  \
service_date                                                           
2022-10-27    528826   439445.857143    446130.833333  112498.919407   
2022-10-28    501985   445055.428571    445172.033333  114808.044086   
2022-10-29    311437   442603.714286    438184.966667  117852.330128   
2022-10-30    217204   439449.428571    428189.133333  124319.151551   
2022-10-31    454862   433130.000000    432284.833333  121882.154625

              Rolling_Sum_7  Rolling_Median_7  
service_date                                   
2022-10-27        3076121.0          478171.0  
2022-10-28        3115388.0          499098.0  
2022-10-29        3098226.0          499098.0  
2022-10-30        3076146.0          499098.0  
2022-10-31        3031910.0          478171.0

4. Visualize results (optional). For the sake of clarity and illustration, we only show the visualization of the two rolling averages calculated in step 2.

import matplotlib.pyplot as plt

plt.figure(figsize=(14, 7))
plt.plot(bus_ridership, label='Daily Bus Ridership', alpha=0.5)
plt.plot(df['Rolling_Mean_7'], label='7-Day Rolling Mean', linewidth=2)
plt.plot(df['Rolling_Mean_30'], label='30-Day Rolling Mean', linewidth=2, linestyle='--')
plt.xlabel('Date')
plt.ylabel('Bus Ridership')
plt.title('Rolling Statistics for Bus Ridership')
plt.legend()
plt.grid()
plt.show()

Visualizing rolling statistics in a time series
Visualizing rolling statistics in a time series

It can be observed that in general, the larger the window size, the smoother the temporal behavior of the rolling average being analyzed.

Wrapping Up

This article showed how to calculate rolling statistics on time series data in Python. The applications of rolling statistics for a more comprehensive analysis of sequential data have a wealth of applications in areas like finance to analyze trends in stock prices, weather forecasting by tracking rainfall over certain periods, and sports to assess player performance over time.

Leave a Reply

Your email address will not be published. Required fields are marked *