添加24小时到python的负时间差 [英] Add 24 hours to a negative time difference in python

查看:618
本文介绍了添加24小时到python的负时间差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



下面是一些解释问题的表:

  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,如果它是否定的:

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






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

 从datetime导入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)

如果d2 < d1:
d2 + = timedelta(days = 1)

打印d2 - d1

#0:35:00

使用熊猫你可以这样做:

  import pandas as pd 
from pandas import Timedelta

d = {
CallDate:[
1/8/2009,
2009年11月11日,
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 [ 'beginning'],format =%Y-%m-%d%H:%M:%S)
df ['EndingTime'] = pd.to_datetime(df ['EndingTime'],format = %Y-%m-%d%H:%M:%S)

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

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






 在[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


I am using python to calculate time intervals between two events. Each event has a 'beginning time' and an 'ending time.' I have found the difference between the two in a new column, 'interval', but have negative values when the beginning and ending time are on different days (for instance begin 23:46:00 and end 00:21:00 gives -23:25:00). I would like to create an if-statement to run through the 'interval' column and add 24 hours to any negative values. However, I have had problems with adding 24 hours to the 'interval' values. Currently my 'interval' dtype=timedelta64[ns].

Here is a little bit of the table to clarify the problem:

        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

Update: Here is the code that had led me to this point

   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']]

解决方案

If you just want to add 1 day to the timedelta if it is negative:

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


If it is safe to make the assumption that the end time will be within 24 hours, you can check to see if the end time is earlier than the start time and use timedelta to add a day to the end time rather than the interval time.

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

With pandas you can do something like this:

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

这篇关于添加24小时到python的负时间差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆