How can I replicate rolling.sum() in Pandas V17

Viewed 31

I am trying to calculate the rolling 3 days sum for the data below:

Date        Qty
01/01/2019  4.15
02/01/2019  12.39
03/01/2019  14.15
04/01/2019  12.15
05/01/2019  3.26
06/01/2019  6.23
07/01/2019  15.89
08/01/2019  5.55
09/01/2019  12.49
10/01/2019  9.4
11/01/2019  9.11
12/01/2019  9.18
13/01/2019  13.45
14/01/2019  4.52

I tried:

data['Rolling_3_day'] = data['Qty'].rolling(3).sum()

But i got the following error:

AttributeError: 'Series' object has no attribute 'rolling'

I think the issue is that .rolling is added for Pandas V18 but I had Pandas V17. But I CAN NOT update pandas now. Is there a way i can calculate rolling sum using V17?

1 Answers

For a small window, you can shift:

df['rolling_3d'] = np.sum([df['Qty'].shift(i) for i in range(3)], axis=0)
Related