Examples

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

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

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

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

In [8]:
df.rolling(1).sum()
Out[8]:
Q
0 0.0
1 2.0
2 4.0
3 NaN
4 6.0

Same as above, but explicitly set the min_periods

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

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

In [15]:
df = pd.DataFrame({'Q': [0, 2, 4, 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[15]:
Q
2019-02-01 09:00:00 0.0
2019-02-01 09:00:02 2.0
2019-02-01 09:00:03 4.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 [16]:
df.rolling('2s').sum()
Out[16]:
Q
2019-02-01 09:00:00 0.0
2019-02-01 09:00:02 2.0
2019-02-01 09:00:03 6.0
2019-02-01 09:00:05 NaN
2019-02-01 09:00:06 6.0