简体   繁体   中英

pandas grouper vs time grouper

The new pandas version deprecates the TimeGrouper , so we should use the regular Grouper .

The old code:

df['column_name'].groupby(pd.TimeGrouper("M")).mean().plot()

works fine in the old version of pandas. However, none of:

df.groupby(pd.Grouper(key='column_name', freq="M")).mean().plot()
df['column_name'].groupby(pd.Grouper(freq="M")).mean().plot()

works in the new version. Eiter the key is considered to be missing, or pandas complains about:

Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Float64Index'

edit

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()

As I said in the comment key should be datetime in grouper. Timegrouper by default converts it to datetime so use

df['column_name'] = pd.to_datetime(df['column_name'])
# new version
df.groupby(pd.Grouper(key='column_name', freq="M")).mean().plot()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM