Window operations
Window operations allow you to calculate values over a sliding partition (or “window”) of values. Commonly, these operations are used to calculate things like “rolling 90-day average,” but they are flexible enough to extend to any algorithm of your choosing.
While not technically a group by operation, window operations are included here as they share a similar API and work with “groups” of data. The only difference to a group by call is that, instead of forming groups from unique value sets, a window operation creates its group by iterating over each value of a pandas object and looking at a particular number of preceding (and sometimes following) values.
How to do it
To get a feel for how window operations work, let’s start with a simple pd.Series
where each element is an increasing power of 2:
ser = pd.Series([0, 1, 2, 4, 8, 16], dtype=pd.Int64Dtype())
ser
0 0
1 1
2 2
3 4
4 ...