I have a dataframe similar to the one below and I want to calculate the sum of the value column for the last seven days. The problem is that there isn't necessarily a row for each day.
df = pd.DataFrame({
'value': [2,3,7,14],
'date': ['10/20/2005','10/22/2005','10/25/2005','10/27/2005']
})
df['date'] = pd.to_datetime(df['date'])
df
value date
2 2005-10-20
3 2005-10-22
7 2005-10-25
14 2005-10-27
What I would like to is something like
df['value'].sum('Last 7 days')
26
The solutions to the problem that I found were always about filling the df with the missing dates, using .asfreq() or .reindex(). Unfortunately, that is not an option for me since I have way too many classes that are each represented in a df like the above one. So filling the df up with the missing dates would create thousands and thousands of extra rows.
Is there a way to use pd.Timedelta() (or similar), where I can treat the missing days as zeros?