How do I get the absolute difference between two datetime.time Pandas columns?

Viewed 24

Say I have this dataframe:

import pandas as pd
import datetime

x = [datetime.time(23,0),datetime.time(6,0),datetime.time(18,0),datetime.time(17,0)]
y = [datetime.time(22,0),datetime.time(9,0),datetime.time(9,0),datetime.time(23,0)]

df = pd.DataFrame({'time1':x,'time2':y})

which looks like this: enter image description here

How would I compute the absolute difference between the two columns? Subtraction doesn't work. The result should look like this:

df['abs_diff'] = [1,3,9,6]

enter image description here

Thanks so much!

1 Answers

Pandas doesn't like datetime objects so very much; it labels the series as object dtype, so you can't really do any arithmetics on those. You can convert the data to Pandas' timedelta:

df['abs_diff'] = (pd.to_timedelta(df['time1'].astype(str))   # convert to timedelta
   .sub(pd.to_timedelta(df['time2'].astype(str)))            # then you can subtract
   .abs().div(pd.Timedelta('1H'))                            # and absolute value, and divide
)

Output:

      time1     time2  abs_diff
0  23:00:00  22:00:00       1.0
1  06:00:00  09:00:00       3.0
2  18:00:00  09:00:00       9.0
3  17:00:00  23:00:00       6.0
Related