gpt4 book ai didi

python - 如何设置时差的最小值?

转载 作者:太空宇宙 更新时间:2023-11-03 14:56:08 25 4
gpt4 key购买 nike

如果出发时间和到达时间之间的时间差小于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或负数)

最佳答案

您可以通过 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

关于python - 如何设置时差的最小值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45562252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com