Search code examples
pythonpython-3.xpandassubtraction

Calculate/subtract the difference between two date time columns


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'

Solution

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