Calculate/subtract the difference between two date time columns

Question:

How can we calculate the difference between two date time columns in Python Pandas? Below is the sample table:

starttime endtime
06:10:42 AM 06:20:00 AM
03:45:54 AM 03:52:27 AM

Desired result:

starttime endtime total
06:10:42 AM 06:20:00 AM 0:10:00
03:45:54 AM 03:52:27 AM 0:06:33

I tried this script. However, it returns an error:

df_null['Total'] = (df_null['endtime']).sub(df_null['starttime'])

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

||

Answers:

Try:

df['total'] = (df[['starttime', 'endtime']].astype(str)
                   .apply(pd.to_datetime).diff(axis=1)['endtime']
                   .astype(str).str[-8:])
print(df)

# Output
      starttime      endtime     total
0   06:10:42 AM  06:20:00 AM  00:09:18
1   03:45:54 AM  03:52:27 AM  00:06:33

In python you can’t subtract two time objects

from datetime import time

start = time(3, 45, 54)
end = time(3, 52, 27)

end - start

...
TypeError: unsupported operand type(s) for -: 'datetime.time' and 'datetime.time'
Answered By: Corralien
df['start'] = pd.to_datetime(df['starttime'])
df['end'] = pd.to_datetime(df['endtime'])

df['total'] = df['end']-df['start']
Answered By: God Is One
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.