Rolling window calculations

import pandas as pd
from pandas import DataFrame, Series
pd.options.display.max_rows = 10

import numpy as np

import matplotlib.pyplot as plt
%matplotlib inline
plt.style.use('seaborn')

Make a noisy time series.

lin = np.linspace(-1, 1, 100)
noise = np.random.normal(0, 0.5, 100)
signal = Series(lin + noise)
signal.plot()
plt.show()

png

Plot the mean for a rolling window containing 10 measurements.

signal.rolling(10).mean().plot()
plt.show()

png

The first 9 measurements are NaN.

signal.rolling(10).mean().head(10)
0         NaN
1         NaN
2         NaN
3         NaN
4         NaN
5         NaN
6         NaN
7         NaN
8         NaN
9   -0.856827
dtype: float64

If you spot any errors please let me know on twitter.