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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33433452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:07:42  来源:igfitidea点击:

Difference between two timestamps in Pandas

pandasdatetime-formattimespan

提问by Nithin Das

I have the following two time column,"Time1" and "Time2".I have to calculate the "Difference" column,which is (Time2-Time1) in Pandas:

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

When Time1 and Time2 are in different hours,I am getting result as"-1 days +" .My desired output for First two values are given below:

当 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

How can I get this output in Pandas?

如何在 Pandas 中获得此输出?

Both time values are in 'datetime64[ns]' dtype.

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

回答by maxymoo

The issue is not that time1and time2are in different hours, it's that time2is before time1so time2-time1is negative, and this is how negative timedeltas are stored. If you just want the difference in minutes as a negative number, you could extract the minutes before calculating the difference:

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

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

回答by Kamil Sindi

I was not able to reproduce the issue using pandas 17.1:

我无法使用 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]