1

I have a pandas dataframe, excerpt shown below, of rainfall data. 'Pcp' is a one day total, which I have then used to calculate rolling cumulative totals of rainfall for the other periods of time leading up to the day of interest (3 days up to 28 days), using:

df['Pcp_3day'] = df['Pcp'].rolling(3).sum()

What I would like to achieve is a rolling total of n days prior to but not including the date of interest. In other words, at the moment, the rolling totals are being formed with rainfall totals from days 0, -1, -2, whereas I would like to exclude day 0 (the day of interest) and have a rolling total of days -1, -2, -3, i.e the three days leading up to it.

I am unsure as to whether this analogy is very clear, but if there is any advice out there, it would be hugely appreciated.

Thanks

                Pcp     Pcp_3day    Pcp_7day    Pcp_10day   Pcp_14day   Pcp_21day   Pcp_28day
date        
2017-12-04      8.382   19.304      21.082      40.132      40.132      42.418      71.374
2017-12-05      12.192  20.574      33.020      42.164      52.324      52.578      81.534
2017-12-06      1.016   21.590      33.020      34.290      53.340      53.594      82.550
2017-12-07      12.700  25.908      45.466      46.990      66.040      66.040      95
2017-12-08      5.080   18.796      50.292      51.816      71.120      71.120      88.900
cs95
  • 379,657
  • 97
  • 704
  • 746
SHV_la
  • 875
  • 1
  • 10
  • 14

1 Answers1

1

Try this:

df['Pcp_3day'] = df['Pcp'].shift().rolling(3).sum()
John Zwinck
  • 239,568
  • 38
  • 324
  • 436