How to compute the rolling mean of a time series in Python

To compute the rolling mean of a time series in Python, you can use the pandas library. This library provides a rolling() method that can be applied to a time series DataFrame, which computes the rolling mean.

In pandas, the rolling() method is used to apply a rolling window function to a time series or a series of data. It allows you to apply a function to a window of data, rather than the entire series. The rolling window function can be any function that takes a group of data as input and returns a single value as output.

Parameters

  • window: the size of the rolling window, which can be an integer, an offset, or a BaseIndexer subclass.

  • min_periods: the minimum number of observations in the window required to have a value.

  • center: a boolean indicating whether the window labels should be set as the center of the window index.

  • win_type: a string specifying a valid Scipy window function.

  • on: a column label or index level on which to calculate the rolling window.

  • axis: the axis along which to roll, either 0 (rows) or 1 (columns).

  • closed: a string indicating whether the first or last point in the window should be excluded from calculations.

  • step: an integer indicating the step size for evaluating the window.

  • method: a string indicating whether the rolling operation should be executed per single column or row ('single') or over the entire object ('table').

Return value

A Window subclass if a win_type is passed or a Rolling subclass if a win_type is not passed.

Example

main.py
airpassengers.csv
import pandas as pd
# load time series data into a DataFrame
df = pd.read_csv('airpassengers.csv')
# compute the rolling mean with a window size of 10
df['rolling_mean'] = df['Passengers'].rolling(3).mean()
# display the first 10 rows of the DataFrame
print(df.head(10))

Explanation

  • Line 1: We import the pandas library.

  • Line 4: We load the time series data into a DataFrame using the read_csv() function from pandas.

  • Line 7: We use the rolling() function to compute the rolling mean with a window size of 3.

  • Line 10: We display the first 10 rows of the DataFrame to check the results.

Free Resources