Skip to content
Advertisement

Pandas add missing weeks from range to dataframe

I am computing a DataFrame with weekly amounts and now I need to fill it with missing weeks from a provided date range.

This is how I’m generating the dataframe with the weekly amounts:

JavaScript

Which outputs:

JavaScript

If a date range is given as start='2020-08-30' and end='2020-10-30', then I would expect the following dataframe:

JavaScript

So far, I have managed to just add the missing weeks and set the sum to 0, but it also replaces the existing values:

JavaScript

Which outputs:

JavaScript

Advertisement

Answer

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

JavaScript

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

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement