Skip to content
Advertisement

Pandas: How to fill missing dates in a long dataframe with multiple non overlapping time series?

I have a long dataframe with multiple timeseries which are non overlapping.

JavaScript

The df looks like this

JavaScript

For the time series with id 1, the missing timestamps are 5,6 and 7 and the time series #2 misses timestamps 0 and 1.

I would like to fill the missing dates for all the time series in the dataframe so all of them have all the dates filled with nan:

JavaScript

The objective is to be able to then reshape this dataframe to a 3d array. Is there a simple way to fill missing dates for each time series? Thanks

Advertisement

Answer

Use Series.unstack with DataFrame.stack:

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