Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding,subtracting datetime.time columns pandas

I have following dataframe

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']

It gives me following error

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

how should I subtract two datetime.time columns?

like image 347
TLanni Avatar asked Oct 16 '25 15:10

TLanni


1 Answers

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

like image 53
CAppajigowda Avatar answered Oct 18 '25 15:10

CAppajigowda