gpt4 book ai didi

pandas - Pandas 中两个时间戳之间的差异

转载 作者:行者123 更新时间:2023-12-05 00:58:09 32 4
gpt4 key购买 nike

我有以下两个时间列,“Time1”和“Time2”。我必须计算 Pandas 中的“差异”列,即 (Time2-Time1):

Time1         Time2                 Difference
8:59:45 9:27:30 -1 days +23:27:45
9:52:29 10:08:54 -1 days +23:16:26
8:07:15 8:07:53 00:00:38

当 Time1 和 Time2 在不同的时间时,我得到的结果为“-1 days +”。我想要的前两个值的输出如下:
Time1         Time2                 Difference
8:59:45 9:27:30 00:27:45
9:52:29 10:08:54 00:16:26

如何在 Pandas 中获得此输出?

两个时间值都在 'datetime64[ns]' dtype 中。

最佳答案

问题不在于 time1time2在不同的时间,就是time2之前 time1所以time2-time1是负数,这就是负时间增量的存储方式。如果您只想将分钟差异作为负数,则可以在计算差异之前提取分钟:

(df.Time1.dt.minute- df.Time2.dt.minute)

关于pandas - Pandas 中两个时间戳之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33433452/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com