pandas add missing weeks to dataframes

Remove reset_index for DatetimeIndex, because reindex working with index and if RangeIndex get 0 values, because no match:

weekly_data = (df.groupby([pd.Grouper(key='date', freq='W-SUN')])[data_type]
                 .sum()
              )

Then is possible use fill_value=0 parameter and last add reset_index:

r = pd.date_range('2020-08-30', '2020-10-30', freq='W-SUN', name='date')
weekly_data = weekly_data.reindex(r, fill_value=0).reset_index()
print (weekly_data)
        date  sum
0 2020-08-30    0
1 2020-09-06    0
2 2020-09-13    0
3 2020-09-20    0
4 2020-09-27    0
5 2020-10-04    0
6 2020-10-11   78
7 2020-10-18  673
8 2020-10-25    0

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top