如何设置时差的最小值?

时间:2017-08-08 07:44:26

标签: python pandas datetime

如果出发时间和到达时间之间的时差小于30分钟或负数,如何假设该值为30?

例如,我有以下数据类型:

id         departure time             arrival time         start  end  capacity

Train A   2016-05-19 01:45:00       2016-05-19 08:25:00   A     G    2
Train A   2016-05-19 16:54:00       2016-05-19 16:00:00   B     H    2
Train A   2016-05-19 21:25:00       2016-05-20 07:25:00   C     I    3
Train B   2016-05-24 02:20:00       2016-05-24 12:50:00   D     J    3
Train B   2016-05-24 18:30:00       2016-05-25 20:00:00   E     K    2
Train B   2016-05-26 19:35:00       2016-05-26 19:45:00   F     L    3

我使用以下公式来查找时差

df['time difference'] = ((df['departure time'] - df['arrival time']).abs())

我想将时间差的最小值设置为30,如果该值小于30或为负)

1 个答案:

答案 0 :(得分:1)

您可以按total_seconds减去列并转换为分钟,然后再分60,然后mask替换为30:

diff = (df['departure time'] - df['arrival time']).dt.total_seconds() / 60
df['time difference'] = diff.mask(diff < 30, 30)
print (df)
        id      departure time        arrival time start end  capacity  \
0  Train A 2016-05-19 01:45:00 2016-05-19 08:25:00     A   G         2   
1  Train A 2016-05-19 16:54:00 2016-05-19 16:00:00     B   H         2   
2  Train A 2016-05-19 21:25:00 2016-05-20 07:25:00     C   I         3   
3  Train B 2016-05-24 02:20:00 2016-05-24 12:50:00     D   J         3   
4  Train B 2016-05-24 18:30:00 2016-05-25 20:00:00     E   K         2   
5  Train B 2016-05-26 19:35:00 2016-05-26 19:45:00     F   L         3   

   time difference  
0             30.0  
1             54.0  
2             30.0  
3             30.0  
4             30.0  
5             30.0