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
  • 22,977
  • 20
  • 73
  • 80
  • Can you share some examples of the "Time variables"? – alec_djinn May 20 '22 at 09:31
  • Does this answer your question? [How do I select rows from a DataFrame based on column values?](https://stackoverflow.com/questions/17071871/how-do-i-select-rows-from-a-dataframe-based-on-column-values) – Nayem Jaman Tusher May 20 '22 at 09:31

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
  • 822,522
  • 95
  • 1,334
  • 1,252