1

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!

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
dcoolwater07
  • 51
  • 1
  • 5
  • Does this answer your question? [Difference between two dates in Pandas DataFrame](https://stackoverflow.com/questions/37583870/difference-between-two-dates-in-pandas-dataframe) – Paul Brennan Sep 22 '22 at 14:02
  • 1
    the thing is that subtracting two times without a date is ambiguous and datetime.time doesn't support vectorization directly – mozway Sep 22 '22 at 14:05

1 Answers1

2

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
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
  • 1
    You couldn't do the arithmetic directly even if pandas liked the datetime.time - in vanilla Python you'd also use something different / convert types, like datetime.datetime or datetime.timedelta – FObersteiner Sep 22 '22 at 15:29