以分钟为单位计算2日期在熊猫中的差异

时间:2018-07-24 06:19:43

标签: python-3.x pandas numpy

我在数据框中有2列。我想以分钟为单位计算两列的差,并将结果写到新列中

Input

Planned Pickup date/time    Actual Pickup date/time      
07/05/2018 09:28:00         07/05/2018 09:33:15               
14/05/2018 17:00:00         15/05/2018 08:44:08               
15/05/2018 17:00:00         15/05/2018 10:52:50              
15/05/2018 17:00:00         15/05/2018 15:03:34             
15/05/2018 17:00:00         15/05/2018 15:03:34              
16/05/2018 17:00:00         16/05/2018 16:00:38   

我想以分钟为单位计算实际和计划的取货差,并将结果写入数据帧的新列data['time difference']

Expected Output

Planned Pickup date/time    Actual Pickup date/time      Time Difference
07/05/2018 09:28:00         07/05/2018 09:33:15               5
14/05/2018 17:00:00         15/05/2018 08:44:08               944
15/05/2018 17:00:00         15/05/2018 10:52:50              -368
15/05/2018 17:00:00         15/05/2018 15:03:34              -117
15/05/2018 17:00:00         15/05/2018 15:03:34              -117
16/05/2018 17:00:00         16/05/2018 16:00:38              -60

这在大熊猫中怎么做

1 个答案:

答案 0 :(得分:1)

使用:

data['time difference'] = ((pd.to_datetime(data['Actual Pickup date/time']) - 
                            pd.to_datetime(data['Planned Pickup date/time']))
                                .dt.total_seconds() / 60)
print (data)

  Planned Pickup date/time Actual Pickup date/time  time difference
0      07/05/2018 09:28:00     07/05/2018 09:33:15         5.250000
1      14/05/2018 17:00:00     15/05/2018 08:44:08       944.133333
2      15/05/2018 17:00:00     15/05/2018 10:52:50      -367.166667
3      15/05/2018 17:00:00     15/05/2018 15:03:34      -116.433333
4      15/05/2018 17:00:00     15/05/2018 15:03:34      -116.433333
5      16/05/2018 17:00:00     16/05/2018 16:00:38       -59.366667

或者如果需要floor值:

data['time difference'] = ((pd.to_datetime(data['Actual Pickup date/time']) - 
                            pd.to_datetime(data['Planned Pickup date/time']))
                                .astype('<m8[m]').astype(int))
print (data)


  Planned Pickup date/time Actual Pickup date/time  time difference
0      07/05/2018 09:28:00     07/05/2018 09:33:15                5
1      14/05/2018 17:00:00     15/05/2018 08:44:08              944
2      15/05/2018 17:00:00     15/05/2018 10:52:50             -368
3      15/05/2018 17:00:00     15/05/2018 15:03:34             -117
4      15/05/2018 17:00:00     15/05/2018 15:03:34             -117
5      16/05/2018 17:00:00     16/05/2018 16:00:38              -60