Examples

In [1]:
import numpy as np
import pandas as pd
In [2]:
df = pd.DataFrame({'Q': [0, 2, 3, np.nan, 6]})
df
Out[2]:
Q
0 0.0
1 2.0
2 3.0
3 NaN
4 6.0

Rolling sum with a window length of 2, using the ‘triang’ window type.

In [3]:
df.rolling(2, win_type='triang').sum()
Out[3]:
Q
0 NaN
1 1.0
2 2.5
3 NaN
4 NaN

Rolling sum with a window length of 2, min_periods defaults to the window length.

In [4]:
df.rolling(2).sum()
Out[4]:
Q
0 NaN
1 2.0
2 5.0
3 NaN
4 NaN

Same as above, but explicitly set the min_periods

In [5]:
df.rolling(2, min_periods=1).sum()
Out[5]:
Q
0 0.0
1 2.0
2 5.0
3 3.0
4 6.0

A ragged (meaning not-a-regular frequency), time-indexed DataFrame

In [6]:
df = pd.DataFrame({'Q': [0, 2, 3, np.nan, 6]},
                  index = [pd.Timestamp('20190201 09:00:00'),
                           pd.Timestamp('20190201 09:00:02'),
                           pd.Timestamp('20190201 09:00:03'),
                           pd.Timestamp('20190201 09:00:05'),
                           pd.Timestamp('20190201 09:00:06')])
df
Out[6]:
Q
2019-02-01 09:00:00 0.0
2019-02-01 09:00:02 2.0
2019-02-01 09:00:03 3.0
2019-02-01 09:00:05 NaN
2019-02-01 09:00:06 6.0

Contrasting to an integer rolling window, this will roll a variable length window corresponding
to the time period. The default for min_periods is 1.

In [7]:
df.rolling('2s').sum()
Out[7]:
Q
2019-02-01 09:00:00 0.0
2019-02-01 09:00:02 2.0
2019-02-01 09:00:03 5.0
2019-02-01 09:00:05 NaN
2019-02-01 09:00:06 6.0