Pandas中两个时间戳之间的差异[英] Difference between two timestamps in Pandas

本文是小编为大家收集整理的关于Pandas中两个时间戳之间的差异的处理方法,想解了Pandas中两个时间戳之间的差异的问题怎么解决?Pandas中两个时间戳之间的差异问题的解决办法?那么可以参考本文帮助大家快速定位并解决问题。

问题描述

i具有以下两个时间列," 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天 +"的形式获得结果.

Time1         Time2                 Difference
8:59:45       9:27:30               00:27:45
9:52:29       10:08:54              00:16:26

如何在熊猫中获取此输出?

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

推荐答案

问题不是time1和time2在不同的时间内,而是time2是time1 time1 so time2-time1是负面的,这就是如何存储负序列的方式.如果您只想在分钟数作为负数中的差异,则可以在计算差异之前提取分钟:

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

其他推荐答案

我无法使用Pandas 17.1:

重现该问题
import pandas as pd

d = {
    "start_time": [
        "8:59:45",
        "9:52:29",
        "8:07:15"
    ],
    "end_time": [
        "9:27:30",
        "10:08:54",
        "8:07:53"
    ]
}

from datetime import  datetime
df = pd.DataFrame(data=d)
df['start_time'] = pd.to_datetime(df['start_time'])
df['end_time'] = pd.to_datetime(df['end_time'])

df.end_time - df.start_time

0   00:27:45
1   00:16:25
2   00:00:38
dtype: timedelta64[ns]

本文地址:https://www.itbaoku.cn/post/1728131.html