简体   繁体   中英

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

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.

Try:

df.groupby(df['Start date'].dt.month_name().rename('month'))

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

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