What are Pandas “expanding window” functions?

You may want to read this Pandas docs:

A common alternative to rolling statistics is to use an expanding
window, which yields the value of the statistic with all the data
available up to that point in time
.

These follow a similar interface to .rolling, with the .expanding
method returning an Expanding object.

As these calculations are a special case of rolling statistics, they
are implemented in pandas such that the following two calls are
equivalent:

In [96]: df.rolling(window=len(df), min_periods=1).mean()[:5]
Out[96]: 
                   A         B         C         D
2000-01-01  0.314226 -0.001675  0.071823  0.892566
2000-01-02  0.654522 -0.171495  0.179278  0.853361
2000-01-03  0.708733 -0.064489 -0.238271  1.371111
2000-01-04  0.987613  0.163472 -0.919693  1.566485
2000-01-05  1.426971  0.288267 -1.358877  1.808650

In [97]: df.expanding(min_periods=1).mean()[:5]
Out[97]: 
                   A         B         C         D
2000-01-01  0.314226 -0.001675  0.071823  0.892566
2000-01-02  0.654522 -0.171495  0.179278  0.853361
2000-01-03  0.708733 -0.064489 -0.238271  1.371111
2000-01-04  0.987613  0.163472 -0.919693  1.566485
2000-01-05  1.426971  0.288267 -1.358877  1.808650

Leave a Comment