python 的负时差加上 24 小时

Add 24 hours to a negative time difference in python

我正在使用 python 来计算两个事件之间的时间间隔。每个事件都有一个 'beginning time' 和一个 'ending time.' 我在一个新列 'interval' 中发现了两者之间的区别,但是当开始和结束时间在不同的日期时它们具有负值(例如 begin 23:46:00 和 end 00:21:00 给出 -23:25:00)。我想通过 'interval' 列为 运行 创建一个 if 语句,并将 24 小时添加到任何负值。但是,我在向 'interval' 值添加 24 小时时遇到了问题。目前我的 'interval' dtype=timedelta64[ns].

这里有一点table来澄清问题:

        CallDate      BeginningTime        EndingTime            Interval
    75  1/8/2009    1900-01-01 07:49:00  1900-01-01 08:19:00     00:30:00
    76  1/11/2009   1900-01-01 14:37:00  1900-01-01 14:59:00     00:22:00
    77  1/9/2009    1900-01-01 09:29:00  1900-01-01 09:56:00     00:27:00
    78  1/11/2009   1900-01-01 09:20:00  1900-01-01 10:13:00     00:53:00
    79  1/16/2009   1900-01-01 15:11:00  1900-01-01 15:50:00     00:39:00
    80  1/17/2009   1900-01-01 22:52:00  1900-01-01 23:26:00     00:34:00
    81  1/19/2009   1900-01-01 05:48:00  1900-01-01 06:32:00     00:44:00
    82  1/20/2009   1900-01-01 23:46:00  1900-01-01 00:21:00     -23:25:00
    83  1/20/2009   1900-01-01 21:29:00  1900-01-01 22:08:00     00:39:00
    84  1/23/2009   1900-01-01 07:33:00  1900-01-01 07:55:00     00:22:00
    85  1/30/2009   1900-01-01 19:33:00  1900-01-01 20:01:00     00:28:00

更新:这是让我走到这一步的代码

   df['BeginningTime']=pd.to_datetime(df['BeginningTime'], format='%H:%M')
   df['EndingTime']=pd.to_datetime(df['EndingTime'], format='%H:%M')

   df['Interval']=df['EndingTime']-df['BeginningTime']

   df[['CallDate','BeginningTime','EndingTime','Interval']]

如果您只想在 timedelta 为负数时增加 1 天:

df['Interval']=df['Interval'].apply(lambda x: x + Timedelta(days=1) if x < 0 else x)

如果假设结束时间在 24 小时内是安全的,您可以检查结束时间是否早于开始时间,并使用 timedelta 将结束时间添加一天而不是比间隔时间。

from datetime import datetime, timedelta

d1 = datetime.strptime( "1900-01-01 23:46:00", "%Y-%m-%d %H:%M:%S" )
d2 = datetime.strptime( "1900-01-01 00:21:00", "%Y-%m-%d %H:%M:%S" )

if d2 < d1:
    d2 += timedelta(days=1)

print d2 - d1

# 0:35:00

使用 pandas 你可以这样做:

import pandas as pd
from pandas import Timedelta

d = {
    "CallDate": [
        "1/8/2009",
        "1/11/2009",
        "1/9/2009",
        "1/11/2009",
        "1/16/2009",
        "1/17/2009",
        "1/19/2009",
        "1/20/2009",
        "1/20/2009",
        "1/23/2009",
        "1/30/2009"
    ],
    "BeginningTime": [
        "1900-01-01 07:49:00",
        "1900-01-01 14:37:00",
        "1900-01-01 09:29:00",
        "1900-01-01 09:20:00",
        "1900-01-01 15:11:00",
        "1900-01-01 22:52:00",
        "1900-01-01 05:48:00",
        "1900-01-01 23:46:00",
        "1900-01-01 21:29:00",
        "1900-01-01 07:33:00",
        "1900-01-01 19:33:00"
    ],
    "EndingTime": [
        "1900-01-01 08:19:00",
        "1900-01-01 14:59:00",
        "1900-01-01 09:56:00",
        "1900-01-01 10:13:00",
        "1900-01-01 15:50:00",
        "1900-01-01 23:26:00",
        "1900-01-01 06:32:00",
        "1900-01-01 00:21:00",
        "1900-01-01 22:08:00",
        "1900-01-01 07:55:00",
        "1900-01-01 20:01:00"
    ]
}

df = pd.DataFrame(data=d)

df['BeginningTime']=pd.to_datetime(df['BeginningTime'], format="%Y-%m-%d %H:%M:%S")
df['EndingTime']=pd.to_datetime(df['EndingTime'], format="%Y-%m-%d %H:%M:%S")

def interval(x):
    if x['EndingTime'] < x['BeginningTime']:
        x['EndingTime'] += Timedelta(days=1)
    return x['EndingTime'] - x['BeginningTime']

df['Interval'] = df.apply(interval, axis=1)

In [2]: df
Out[2]:
         BeginningTime   CallDate          EndingTime  Interval
0  1900-01-01 07:49:00   1/8/2009 1900-01-01 08:19:00  00:30:00
1  1900-01-01 14:37:00  1/11/2009 1900-01-01 14:59:00  00:22:00
2  1900-01-01 09:29:00   1/9/2009 1900-01-01 09:56:00  00:27:00
3  1900-01-01 09:20:00  1/11/2009 1900-01-01 10:13:00  00:53:00
4  1900-01-01 15:11:00  1/16/2009 1900-01-01 15:50:00  00:39:00
5  1900-01-01 22:52:00  1/17/2009 1900-01-01 23:26:00  00:34:00
6  1900-01-01 05:48:00  1/19/2009 1900-01-01 06:32:00  00:44:00
7  1900-01-01 23:46:00  1/20/2009 1900-01-01 00:21:00  00:35:00
8  1900-01-01 21:29:00  1/20/2009 1900-01-01 22:08:00  00:39:00
9  1900-01-01 07:33:00  1/23/2009 1900-01-01 07:55:00  00:22:00
10 1900-01-01 19:33:00  1/30/2009 1900-01-01 20:01:00  00:28:00