Doing a groupby and rolling window on a Pandas Dataframe with a multilevel index leads to a duplicated index entry
It is bug.
But version with apply
works nice, this alternative is here (only d1
was moved to apply
):
df = df.groupby(level='id').d1.apply(lambda x: x.rolling(window=2).sum())
print(df)
id date
1 1 NaN
2 40.0
3 80.0
2 1 NaN
2 42.0
3 82.0
Name: d1, dtype: float64