0

I have daily covid data that I would like to roll up to weekly. The problem is that I want my weeks to go from Sunday to Saturday, but the default is Mon to Sun. I tried to use loffset, but it's only changing the dates not my data, plus it's adding a date that does not exist in the dataset.

Code:

logic = {'iso_code'  : 'first',
         'new_cases'  : 'sum',
         'new_deaths'  : 'sum',
         'icu_patients'   : 'sum',
         'hosp_patients' : 'sum',
         'people_vaccinated': 'sum'} #it's possible to have 'first', 'max','last', etc

offset = pd.offsets.DateOffset(-1)

df_covid_weekly = df_covid_file.resample('W', on='date', label = 'right', loffset=offset).apply(logic).reset_index()

Raw Data Snipet: enter image description here

Current Outcome: enter image description here

Expected Outcome: enter image description here

Jonathan Hay
  • 195
  • 11

1 Answers1

2

Use anchored offsets:

df_covid_file.resample('W-SAT', on='date', label = 'right')

The offset W is equivalent to W-SUN ("week ending on Sunday") and W-SAT is "week ending on Saturday", and so on.

If you want an offset object you can use pd.offsets.Week(weekday=5), which is equivalent to W-SAT. The offset strings are aliases for these objects. Sometimes using the objects instead of their string counterparts makes code parametrization a little easier.

dicristina
  • 335
  • 2
  • 13