1

I have a time variable in my dataframe and I am trying to come up with a new column that gives the time passed in minutes from one row to another. I was trying to use this function:

df["Time"].diff()

But I get the following error:

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

Is there any way I can create a column in pandas with the time difference from one row to the next one?

RobC
  • 20,007
  • 20
  • 62
  • 73

1 Answers1

2

You can convert Time column to datetimes with default same date, so Series.diff working well:

df['difference'] = pd.to_datetime(df["Time"].astype(str)).diff()
jezrael
  • 729,927
  • 78
  • 1,141
  • 1,090