Pandas MultiIndex DataFrame.rolling offset

后端 未结 1 926
半阙折子戏
半阙折子戏 2020-12-20 17:15

Why can\'t I use an offset when rolling a multi-index DataFrame? For example, with:

rng = pd.date_range(\'2017-01-03\', period         


        
相关标签:
1条回答
  • 2020-12-20 17:58

    In order to use an offset like '30D' you need a simple date index. In this case the simplest way to achieve that is to move 'Name' out of the index with reset_index(level='Name'), leaving you with only 'Date' as the index:

    df['Avg'] = df.reset_index(level='Name').groupby(['Name'])['Vals'].rolling('30D').mean()
    
    0 讨论(0)
提交回复
热议问题