10

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()
Georg Heiler
  • 16,916
  • 36
  • 162
  • 292

1 Answers1

15

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()
Bharath M Shetty
  • 30,075
  • 6
  • 57
  • 108