Python Forum
Getting the difference between two times in a Data Frame
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?
Reply
#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)
Reply
#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

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#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.
Reply
#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

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to filter data frame dynamically with the columns psahay 0 298 Aug-24-2020, 01:10 PM
Last Post: psahay
  Dropping Rows From A Data Frame Based On A Variable JoeDainton123 1 373 Aug-03-2020, 02:05 AM
Last Post: scidam
  How to shift data frame rows of specified column Mekala 0 540 Jul-21-2020, 02:42 PM
Last Post: Mekala
  HELP- DATA FRAME INTO TIME SERIES- BASIC bntayfur 0 329 Jul-11-2020, 09:04 PM
Last Post: bntayfur
  Pandas data frame creation from Kafka Topic vboppa 0 303 Jul-01-2020, 04:23 PM
Last Post: vboppa
  Filter rows by multiple text conditions in another data frame i.e contains strings an Pan 0 544 Jun-09-2020, 06:05 AM
Last Post: Pan
  Displaying Result from Data Frame from Function eagle 1 531 Apr-08-2020, 11:58 PM
Last Post: eagle
  add formatted column to pandas data frame alkaline3 0 439 Mar-22-2020, 06:44 PM
Last Post: alkaline3
  reformatting data frame tkirkland 0 361 Mar-18-2020, 02:17 PM
Last Post: tkirkland
  How to modify data frame row value SriRajesh 0 381 Feb-12-2020, 05:46 PM
Last Post: SriRajesh

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020