pandas rolling cumsum over the trailing n elements

Viewed 8647

Using pandas, what is the easiest way to calculate a rolling cumsum over the previous n elements, for instance to calculate trailing three days sales:

df = pandas.Series(numpy.random.randint(0,10,10), index=pandas.date_range('2020-01', periods=10))
df
2020-01-01    8
2020-01-02    4
2020-01-03    1
2020-01-04    0
2020-01-05    5
2020-01-06    8
2020-01-07    3
2020-01-08    8
2020-01-09    9
2020-01-10    0
Freq: D, dtype: int64

Desired output:

2020-01-01     8
2020-01-02    12
2020-01-03    13
2020-01-04     5
2020-01-05     6
2020-01-06    13
2020-01-07    16
2020-01-08    19
2020-01-09    20
2020-01-10    17
Freq: D, dtype: int64
1 Answers
Related