Pandas timedelta 仅计算时间(忽略日期)?
我有一堆格式为:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据doc,属性
秒给你
查看文档以了解您感兴趣的属性/方法。
According to doc, the attribute
seconds
gives youCheck out the doc for attributes/methods of your interest.