Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

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

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?

question from:https://stackoverflow.com/questions/66057294/how-can-i-replicate-rolling-sum-in-pandas-v17

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For a small window, you can shift:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...