![]() |
Getting the difference between two times in a Data Frame - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: Getting the difference between two times in a Data Frame (/thread-16930.html) |
Getting the difference between two times in a Data Frame - soulkid555 - Mar-20-2019 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.timeTo get the minimum time difference I use below, but I get
print("Minimum difference is : ", (Column1Times - Column2Times).min())Can someone please tell me what I am doing wrong? RE: Getting the difference between two times in a Data Frame - scidam - Mar-21-2019 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) RE: Getting the difference between two times in a Data Frame - perfringo - Mar-21-2019 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) RE: Getting the difference between two times in a Data Frame - scidam - Mar-21-2019 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.
RE: Getting the difference between two times in a Data Frame - perfringo - Mar-21-2019 (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 ![]() |