I have data on frequencies (N
), for combinations of [from, to, subset], and the month. Importantly, when N=0, the row is missing.
N from to subset
month
1996-01-01 8.956799 1 2 0
1996-02-01 2.068997 1 2 0
1996-03-01 1.086952 1 2 0
1996-05-01 7.103955 1 2 0
I want to compute a rolling average of N over these, considering the zeros.
df.groupby(['from', 'to', 'subset.rolling(3, center=True).mean()
This works, but does not treat missing rows as zeros. So, I first have to fill the misisng rows.
df.reset_index().set_index(['month', 'from', 'to', 'subset']).resample('1M', level=0).fillna(0)
This does not work, and gives me ValueError: Upsampling from level= or on= selection is not supported, use .set_index(...) to explicitly set index to datetime-like
.
Can I not upsample for a multi-index? Should I try a different approach altogether?