0

I have a column in my dataframe formatted like 11/24/2020 7:09:45. I would like to create another column that is the delta in seconds between the previous row and current row. I may be making this way more complicated than it needs to be.

import pandas as pd
import numpy as np

df
run     run_ts
11:02   11/30/2020 7:24:00
10:11   11/30/2020 7:23:00
16:08   11/30/2020 7:35:00
14:33   11/30/2020 7:32:00
14:27   11/30/2020 7:25:45
14:26   11/30/2020 7:25:25
14:25   11/30/2020 7:25:10
14:24   11/30/2020 7:24:55
NaaN    11/30/2020 7:24:15
NaaN    11/30/2020 7:24:30

df['timeDiff'] = np.where(df['run'] == "NaaN", df['run_ts'].shift(1)-df['run_ts']), "")

TypeError: unsupported operand type(s) for -: 'str' and 'str'

I am not certain which direction to take this. Thanks for the help!

PM 77-1
  • 12,933
  • 21
  • 68
  • 111
jpow
  • 11
  • 1
  • Does this answer your question? [In Python, how do you convert a \`datetime\` object to seconds?](https://stackoverflow.com/questions/7852855/in-python-how-do-you-convert-a-datetime-object-to-seconds) – Danilo Ivanovic Nov 25 '20 at 00:25
  • 1
    Does this answer your question? [Python - Convert datetime column into seconds](https://stackoverflow.com/questions/40992976/python-convert-datetime-column-into-seconds) – Mr. T Nov 25 '20 at 02:29

2 Answers2

0

Try .total_seconds(). See the answer to the identical question here.

Marko
  • 103
  • 5
0

parse to_datetime, take the diff and then the total_seconds of that. Ex:

df['run_ts'] = pd.to_datetime(df['run_ts'], format="%m/%d/%Y %H:%M:%S")

df['diff_to_prev[s]'] = df['run_ts'].diff().dt.total_seconds()

df
     run              run_ts  diff_to_prev[s]
0  11:02 2020-11-30 07:24:00              NaN
1  10:11 2020-11-30 07:23:00            -60.0
2  16:08 2020-11-30 07:35:00            720.0
3  14:33 2020-11-30 07:32:00           -180.0
...
FObersteiner
  • 22,500
  • 8
  • 42
  • 72