Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Getting the difference between two times in a Data Frame
#1
I have a Pandas Dataframe where two columns contain times (stored as strings).

I want to get the minimum difference (hours, minutes, seconds) between both columns (Column1 and Column2).

I have converted both columns to times as per below and stored them in different dataframes:

Column1Times = pd.to_datetime(myDataFrame['Column1'], format='%H:%M:%S').dt.time
Column2Times = pd.to_datetime(myDataFrame['Column2'], format='%H:%M:%S').dt.time
To get the minimum time difference I use below, but I get
Error:
operand "-" not suuported.
print("Minimum difference is : ", (Column1Times - Column2Times).min())
Can someone please tell me what I am doing wrong?
Yoriz wrote Mar-20-2019, 06:24 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Quote
#2
You can only find differences for datetime objects, try this:

(Column1Times - Column2Times).apply(lambda x: abs(x.days * 86400 * 10**6 + x.seconds * 10**6 + x.microseconds)%(86400 * 10**6) / 10**6) 
# note absolute values used (result in seconds)
Quote
#3
I tried following:

>>> import pandas as pd
>>> Column1Times = pd.to_datetime('10:10:10', format='%H:%M:%S')
>>> Column2Times = pd.to_datetime('11:12:13', format='%H:%M:%S')
>>> delta = Column2Times - Column1Times
>>> delta
Timedelta('0 days 01:02:03')
>>> delta.seconds
3723
>>> delta.total_seconds()      
3723.0
>>> delta.components
Components(days=0, hours=1, minutes=2, seconds=3, milliseconds=0, microseconds=0, nanoseconds=0)
>>> delta.components.hours, delta.components.minutes, delta.components.seconds
(1, 2, 3)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Life of Brian: Conjugate the verb, "to go" !
Quote
#4
I am trying to remember why I did create so sophisticated answer, but no success; In any case, I think that the expected by the OP answer would be something like min(delta.seconds, (-delta).seconds), where delta is difference between two datetime objects. So, the difference between two times 23:59:00 and 00:00:01 should be 61 sec.
Quote
#5
(Mar-21-2019, 10:54 AM)scidam Wrote: I am trying to remember why I did create so sophisticated answer, but no success;

Been there, done that Smile
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Life of Brian: Conjugate the verb, "to go" !
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to find difference between two timestamp index python_newbie09 3 152 Aug-01-2019, 10:24 AM
Last Post: python_newbie09
  Insert Pandas Data Frame into Teradata DB kylenater 0 273 Jul-19-2019, 04:53 PM
Last Post: kylenater
  tell exact difference between xlim() and xticks() function ift38375 3 181 Jul-12-2019, 12:04 AM
Last Post: scidam
  Creating new rows and adding them to empty data frame kapilan15 0 196 May-31-2019, 10:19 AM
Last Post: kapilan15
  OpenCV - extract 1st frame out of a video file kerzol81 2 1,374 Nov-12-2018, 09:12 AM
Last Post: kerzol81
  Adding times in ISO format? amca01 1 478 Oct-31-2018, 11:49 PM
Last Post: Larz60+
  save video frames into pandas data-frame tofi 0 405 Oct-18-2018, 07:02 PM
Last Post: tofi
  Best way to append data frame? WuchaDoin 1 494 Oct-04-2018, 07:17 PM
Last Post: volcano63
  How to perform my code multipel times Raj 1 515 May-03-2018, 11:30 AM
Last Post: j.crater
  Reading json file as pandas data frame? Alberto 1 4,568 Feb-05-2018, 12:43 AM
Last Post: snippsat

Forum Jump:


Users browsing this thread: 1 Guest(s)