新的 pandas 版本弃用了TimeGrouper
,所以我们应该使用常规的Grouper
.
旧代码:
df['column_name'].groupby(pd.TimeGrouper("M")).mean().plot()
在旧版本的熊猫中工作正常。但是,没有一个:
df.groupby(pd.Grouper(key='column_name', freq="M")).mean().plot()
df['column_name'].groupby(pd.Grouper(freq="M")).mean().plot()
在新版本中工作。要么 key 被认为丢失,要么 pandas 抱怨:
Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Float64Index'
编辑
import pandas as pd
df = pd.DataFrame({'column_name':['2017-01-01', '2017-01-02'],
'column_value':[1,3]})
df
df.index = pd.DatetimeIndex(df.column_name)
df.index
# old version
df['column_value'].groupby(pd.TimeGrouper("M")).mean().plot()
# new version
df.groupby(pd.Grouper(key='column_value', freq="M")).mean().plot()