I would like a daily breakdown of a dataset by type. There are not records for every day for every type, where they don't exist I would like NaN.
I'm able to get a 'resampled to daily' result, but the type is ommitted.
The code below should be a fully working example (well, working apart from the known error at the end!):
import pandas as pd
import datetime as dt
df = pd.DataFrame({
'Date': [dt.datetime(2021,1,1), dt.datetime(2021, 1, 3), dt.datetime(2020,1,2)],
'Type': ['A', 'A', 'B'],
'Value': [1,2,3]
})
df.set_index('Date', inplace=True)
# this loses the 'type'
print(df.resample('1D').mean())
df = df.reset_index().set_index(['Date', 'Type'])
# this raises an exception "TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'MultiIndex'"
print(df.resample('1D').mean())
Output I'm looking for is a row for every day / type combo:
date | type | value |
---|---|---|
20210101 | A | 1 |
20210102 | A | NaN |
20210103 | A | 2 |
20210101 | B | NaN |
20210102 | B | 3 |
20210103 | B | NaN |
Any advice or pointers gratefully received.