从熊猫中的datetime对象提取小时

发布于 2025-01-22 03:40:16 字数 71 浏览 2 评论 0原文

当我使用dt.hour在Pandas中提取23.35小时时,它会返回0。但是我想获得24。如何做?

When I extract hours from 23.35 using dt.hour in pandas it returns 0. But i want to get 24. How to do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

生死何惧 2025-01-29 03:40:16

假设此输入:

s = pd.to_datetime(pd.Series(['00:10', '12:30', '23:35']))
0   2022-04-18 00:10:00
1   2022-04-18 12:30:00
2   2022-04-18 23:35:00
dtype: datetime64[ns]

确实是一个小时

s.dt.hour
0     0
1    12
2    23
dtype: int64

,如果您round

s.dt.round('h').dt.hour
0     0
1    12
2     0
dtype: int64

可以做什么以转换圆形小时+分钟

s.dt.hour.add(s.dt.minute/60).round().astype(int)
0     0
1    12
2    24
dtype: int64

Assuming this input:

s = pd.to_datetime(pd.Series(['00:10', '12:30', '23:35']))
0   2022-04-18 00:10:00
1   2022-04-18 12:30:00
2   2022-04-18 23:35:00
dtype: datetime64[ns]

hours

s.dt.hour
0     0
1    12
2    23
dtype: int64

An indeed, if you round:

s.dt.round('h').dt.hour
0     0
1    12
2     0
dtype: int64

what you can do it to convert rounded hour+minutes

s.dt.hour.add(s.dt.minute/60).round().astype(int)
0     0
1    12
2    24
dtype: int64
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文