添加,减去 datetime.time 列 pandas [英] adding,subtracting datetime.time columns pandas

查看:48
本文介绍了添加,减去 datetime.time 列 pandas 的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据框

flight_departure   arrival_at_desination   boarding  total_flight_time   total_flight_time/2    time_to_collect_bags
0:00                     4:00               23:30           4:00                  2:00                     4:30
9:00                     14:30              8:30            5:30                  2:45                      15:00

flight_departure- 0:00 signifies 12:00 AM
arrival_at_desination- 4:00 signifies 4 AM
boarding = flight_departure-30 minutes(23:30)
total_flight_time=arrival_at_desination-flight_departure(4 hours)

total_flight_time/2-calculates hald time(2 hours in this case)
time_to_collect_bags=arrival_at_desination+30 minutes(4:30AM)

当我尝试执行以下操作时

When I try to do the following

df['arrival_at_desination']-df['flight_departure']

它给了我以下错误

TypeError: unsupported operand type(s) for -: 'datetime.time' and 'datetime.time'

我应该如何减去两个 datetime.time 列?

how should I subtract two datetime.time columns?

推荐答案

你必须转换成datatime格式才能转换试试这个

You have to convert to datatime format to convert try this

arrival = pd.to_datetime(df['arrival_at_desination'])
dept = pd.to_datetime(df['flight_departure'])
diff = arrival - dept

这就是我得到的,希望能帮到你,

This is what I get, hope this help,

0   -1 days +20:00:00
1   -1 days +18:30:00

否则在数据中添加日期,与时间连接并执行上述操作

Else add date to the data, concatenate with time and perform the above

这篇关于添加,减去 datetime.time 列 pandas 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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