I have a dataframe with an index of (month, A, B):
foo N
month A B
1983-03-01 3 9 0 1
1983-06-01 3 9 0 1
1983-09-01 3 9 0 1
1983-11-01 4 5 0 1
1984-05-01 4 5 0 1
1984-06-01 3 9 0 1
1984-09-01 3 9 0 2
I would like to fill all missing dates, provided that a certain (A, B) combination exists in the index. What I do not want to do is to fill in the index for all (A, B) combinations.
That is, I would like to have for (A=3, B=9) and for (A=4, B=5) month-indices running from 1983-03-01 to 1984-09-01 and 0s for filling. But I don’t want there to be any records of (A=3, B=5) or (A=4, B=9).
If this was a single index, I could simply
idx = pd.date_range(df['month'].min(), df['month'].max(), freq='M')
df = df.set_index('month')
df.index = df.reindex(idx, fill_value=0)
How would I approach it in this situation?