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?
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'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With