熊猫滚动日期时间不接受日期时间偏移
我的数据框如上面所示。 dtypes 是
weekday int64
date datetime64[ns]
time object
customers int64
dtype: object
我想将客户列求和为过去 2 小时到达的客户数量(存储在日期列中)。但是,使用 Pandas Rolling 功能,我只能写
df['customers'] = df['date'].rolling(2).count()
This only counts the previous两行日期,完全忽略日期时间值。我想写
df['customers'] = df['date'].rolling('2H').count() #desired: 2H
以获得正确的结果。但是,我收到ValueError:窗口必须是整数
。从 pandas 读取滚动文档,日期时间对象应该能够接收滚动时间窗口(https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.rolling.html)。我完全不知道为什么我的日期时间列不能使用此功能。
My dataframe is presented above. The dtypes are
weekday int64
date datetime64[ns]
time object
customers int64
dtype: object
I'd like to sum the customers column to be the count of customers arrived in the past 2 hours (stored in column date). However, using the Pandas Rolling functionality, I can only write
df['customers'] = df['date'].rolling(2).count()
This only counts the previous two date rows completely disregarding datetime values. I'd like to write
df['customers'] = df['date'].rolling('2H').count() #desired: 2H
to get the correct result. However, I'm getting ValueError: window must be an integer
. Reading the rolling documentation from pandas, a datetime object should be able to receive a rolling time window (https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.rolling.html). I'm completely clueless why my datetime column cannot use this functionality.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建排序的
DatetimeIndex
:Create sorted
DatetimeIndex
: