Search code examples
pythonpandasdatetimepython-datetime

How to take difference between datetime.times in a pandas DataFrame column


I have a df with some times:

import datetime as dt
import pandas as pd

data = [dt.time(0,0,10), dt.time(0,24,30), dt.time(4,20,12)]

df = pd.DataFrame(data, columns=['times'])

How can I use the logic from .diff() to take the difference for datetimes like below where the first entry in the 'gap' row will be blank, and the next will be the difference between the second row and first row's 'time' value, and so on?

df['gap'] = df['times'].diff()

As of now I am getting an error

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


Solution

  • You have to convert data to a list of datetime.datetime's:

    data = [dt.time(0,0,10), dt.time(0,24,30), dt.time(4,20,12)]
    data = [dt.datetime.combine(dt.date.today(), d) for d in data]
    
    df = pd.DataFrame(data, columns=['times'])
    df.times.diff()
    

    Output

    0               NaT
    1   0 days 00:24:20
    2   0 days 03:55:42