简体   繁体   English

pandas 不能按日期分组,仅对 DatetimeIndex、TimedeltaIndex 或 PeriodIndex 有效,但

[英]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:如果您的索引不是日期时间类型,则需要使用pd.Grouper设置键:

df.groupby(pd.Grouper(key="Start date", freq='M'))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pandas重采样:TypeError:仅对DatetimeIndex,TimedeltaIndex或PeriodIndex有效,但得到'RangeIndex'的实例 - Pandas Resampling: TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex' Pandas TypeError:仅对DatetimeIndex,TimedeltaIndex或PeriodIndex有效,但具有“ Int64Index”的实例 - Pandas TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Int64Index' Pandas dataframe.resample TypeError '仅对 DatetimeIndex、TimedeltaIndex 或 PeriodIndex 有效,但获得了“RangeIndex”实例 - Pandas dataframe.resample TypeError 'Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex' 类型错误:仅对 DatetimeIndex、TimedeltaIndex 或 PeriodIndex 有效,但得到了“RangeIndex”的实例 - TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex' Pandas 重采样错误:仅对 DatetimeIndex、TimedeltaIndex 或 PeriodIndex 有效 - Pandas Resampling error: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex 类型错误:如何修复仅对 DatetimeIndex、TimedeltaIndex 或 PeriodIndex 有效,但得到了“Index”的实例 - TypeError: How to fix Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Index' TypeError:仅当dtype为datetime64 [ns]时对DatetimeIndex,TimedeltaIndex或PeriodIndex有效 - TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex when dtype is datetime64[ns] Python datetime 仍然给出“TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Index'” - Python datetime still gives "TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Index'" TypeError:仅对 DatetimeIndex、TimedeltaIndex 或 PeriodIndex 有效,但得到了“RangeIndex”的实例,我不知道为什么 - TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex' and I can't figure out why 每半小时可以使用熊猫TimeDeltaIndex,PeriodIndex或DateTimeIndex吗? - For half-hourly intervals can I use Pandas TimeDeltaIndex, PeriodIndex or DateTimeIndex?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM