0

I have dataframe with two cols. Both of them are timestamp however one goes all the way to fff where other to the seconds. is there a way to get difference in minutes and seconds?

col1            col2
2:21:40.756     2:21:41
2:22:30.343     2:22:31
2:24:43.342     2:24:44

i have tried following:
col1= pd.todatetime(df.col1,format='%H:%M:%S.%f').dt.time
col2 = pd.todatetime(df.col2,format = '%H:%M:%S').dt.time
diff = col1-col2
how ever im getting error -: not supported from datetime.date to datetime.date
gannu
  • 105
  • 1
  • 9
  • Does this answer your question? [Pandas: Subtracting two date columns and the result being an integer](https://stackoverflow.com/questions/37840812/pandas-subtracting-two-date-columns-and-the-result-being-an-integer) – Juan C Apr 08 '20 at 18:28
  • `col1.sub(col2)` should do the trick – Cory Kramer Apr 08 '20 at 18:28
  • not really. i have two cols one goes all the way to FFF where other to second. Im looking for answers in minutes. – gannu Apr 08 '20 at 18:32
  • @CoryKramer i tried and iam getting values like -1 days +23:59:59.875000 is there way to convert these into the minutes and seconds? – gannu Apr 08 '20 at 18:34

1 Answers1

0

You can use, as @CoryKramer said, .sub:

In minutes:

col1.sub(col2).dt.seconds/60

0    1439.983333
1    1439.983333
2    1439.983333
dtype: float64

If you want more precise, as microseconds:

col1.sub(col2).dt.microseconds

0    756000
1    343000
2    342000
dtype: int64

Bruno Mello
  • 4,448
  • 1
  • 9
  • 39