pandas cannot group by date, Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but

Question:

Hello I have the following code:

import pandas as pd
df = pd.read_csv('https://assets.datacamp.com/production/repositories/3551/datasets/181c142c56d3b83112dfc16fbd933fd995e80f94/capital-onebike.csv',
                 parse_dates = ['Start date', 'End date'])
df.dtypes

Return this

Start date              datetime64[ns]
End date                datetime64[ns]
Start station number             int64
Start station                   object
End station number               int64
End station                     object
Bike number                     object
Member type                     object
dtype: object

But when I try to group by month with the following code:

 df['Start date'].groupby(pd.Grouper(freq='M'))

or

 df.groupby(pd.Grouper(freq='M'))

I got the following error:

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

How can I solve this?

Thanks.

Asked By: Tlaloc-ES

||

Answers:

Try:

df.groupby(df['Start date'].dt.month_name().rename('month'))
Answered By: Crapicus

with pd.Grouper you need to set the key, if your index is not a datetime type:

df.groupby(pd.Grouper(key="Start date", freq='M'))
Answered By: s_pike
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.