Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I group date by month using pd.Grouper?

I've searched stackoverflow to find out how to group DateTime by month and for some reason I keep receiving this error, even after I pass the dataframe through pd.to.datetime

TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Int64Index'

df['Date'] = pd.to_datetime(df['Date'])
df['Date'].groupby(pd.Grouper(freq='M'))

When I pull the datatype for df['Date'] it shows dtype: datetime64[ns] Any, ideas why I keep getting this error?

like image 804
prime90 Avatar asked Jan 27 '23 12:01

prime90


1 Answers

The reason is simple: you didn't pass a groupby key to groupby.

What you want is to group the entire dataframe by the month values of the contents of df['Date'].

However, what df['Date'].groupby(pd.Grouper(freq='M')) actually does is first extract a pd.Series from the DataFrame's Date column. Then, it attempts to perform a groupby on that Series; without a specified key, it defaults to attempting to group by the index, which is of course numeric.

This will work:

df.groupby(pd.Grouper(key='Date', freq='M'))

like image 79
gmds Avatar answered Jan 29 '23 00:01

gmds