熊猫滚动日期时间不接受日期时间偏移

发布于 2025-01-12 18:23:02 字数 907 浏览 0 评论 0原文

我的数据框如下:

我的数据框如上面所示。 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 the following:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抱猫软卧 2025-01-19 18:23:02

创建排序的DatetimeIndex

df['date'] = pd.to_datetime(df['date'])
df = df.set_index('date').sort_index()
df['customers'] = df['customers'].rolling('2H').count()

Create sorted DatetimeIndex:

df['date'] = pd.to_datetime(df['date'])
df = df.set_index('date').sort_index()
df['customers'] = df['customers'].rolling('2H').count()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文