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

标签 pandas datetime-format timespan

我有以下两个时间列,“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/

相关文章:

sql-server - Excel中的SQL Server Datetime对象持续重新格式化问题

Spring:如何从属性文件设置 @DateTimeFormat 的模式?

python - 值错误 : cannot copy sequence with size 821 to array axis with dimension 7

pandas - 用于时间序列转换的 Pandas 中的 EWM

python - 值错误: time data '12/31/2017' does not match format '%d-%m-%Y'

c# - 如何格式化时间跨度以显示总小时数?

C# TimeSpan.FromTicks() 不准确?

c# - 什么是计算时间跨度的最佳方法

python - 读取和存储 JSON 数据的有效方法

python - 从包含名称+日期的字符串中提取日期