2
我正在使用python來計算兩個事件之間的時間間隔。每個事件都有一個「開始時間」和一個「結束時間」。我在新的專欄'間隔'中找到了兩者之間的差異,但是當開始和結束時間在不同日期時具有負值(例如開始23:46:00和結束00:21:00給出-23 :25:00)。我想創建一個if語句來運行「間隔」列,並將24小時添加到任何負值。不過,我在將24小時添加到「間隔」值時遇到了問題。目前我的'間隔'dtype = timedelta64 [ns]。在python中添加24小時以消極時差
下面是表的一點點澄清問題:
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']]
粘貼代碼。 – bosnjak