Pandas timedelta 仅计算时间(忽略日期)?

发布于 2025-01-12 15:40:17 字数 773 浏览 0 评论 0原文

我有一堆格式为:

0    2021-12-07 12:42:56
1    2021-12-14 12:20:28
2    2022-02-03 13:41:33
3    2022-02-17 10:07:37
4    2022-02-22 13:02:02


0    2021-14-07 13:00:13
1    2021-12-14 12:39:01
2    2022-05-03 13:00:53
3    2022-02-17 10:45:23
4    2022-02-22 13:17:41 

事件的开始和结束的日期。我需要计算持续时间:

    t1 = pd.to_datetime(for_test['Start'])
    t2 = pd.to_datetime(for_test['End'])
    list_of_time_difference = []
    list_of_ind = []
    for i in range (0, len (t2)): 
        list_of_time_difference.append(pd.Timedelta(t2[i] - t1[i]).seconds / 3600.0)
        if list_of_time_difference[-1] > 24: 
            list_of_ind.append(i) 

但看起来,当我计算时差时忽略了日期,而只考虑时间。我希望 timedelta 能够给出两个日期和时间之间的差异

有没有办法解决这个问题?

I have a bunch of dates of the format :

0    2021-12-07 12:42:56
1    2021-12-14 12:20:28
2    2022-02-03 13:41:33
3    2022-02-17 10:07:37
4    2022-02-22 13:02:02


0    2021-14-07 13:00:13
1    2021-12-14 12:39:01
2    2022-05-03 13:00:53
3    2022-02-17 10:45:23
4    2022-02-22 13:17:41 

for the beginning and the end of the event. I need to calculate duration:

    t1 = pd.to_datetime(for_test['Start'])
    t2 = pd.to_datetime(for_test['End'])
    list_of_time_difference = []
    list_of_ind = []
    for i in range (0, len (t2)): 
        list_of_time_difference.append(pd.Timedelta(t2[i] - t1[i]).seconds / 3600.0)
        if list_of_time_difference[-1] > 24: 
            list_of_ind.append(i) 

But it looks that when I calculate the time difference ignoring the date, and take into consideration time only. I had hope that timedelta would give the difference between two dates and time

Is there a way to fix this?

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

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

发布评论

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

评论(1

dawn曙光 2025-01-19 15:40:17

根据doc,属性秒给你

秒数(>= 0 且小于 1 天)。

查看文档以了解您感兴趣的属性/方法。

According to doc, the attribute seconds gives you

Number of seconds (>= 0 and less than 1 day).

Check out the doc for attributes/methods of your interest.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文