Python Forum
How to get indices of minimum time difference
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get indices of minimum time difference
#1
Hi,
I have below data:
df1:
time1 time2
2020/11/05 20:30:23 2020/11/05 20:20:23
2020/11/05 20:40:12 2020/11/05 20:20:23
2020/11/05 20:49:53 2020/11/05 20:20:23
2020/11/05 20:56:28 2020/11/05 20:20:23

df2:

time
2020/11/05 20:36:23

I want to get df1 row indices (1) time2 > time, 2. time1 should be close (minimum difference).

How to get this.

I can only filter

df1_filter = df1[df1['time1'] < df2['time'], but after this I sruck how to get match the (1) time2 > time, 2. time1 should be close (minimum difference).

Please kindly someone help,
Reply
#2
The first thing you need to do is extract the datetime strings from the lines. Can you do that?

To determine the shortest duration between the two times you need to convert them to something you can subtract. Any units will do, hours, days, seconds, as long as both times are converted to the same time unit. Even a datetime object will work. Can you do this?

The last part is the easiest. Calculate the duration for each line and keep track of the shortest duration.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to get index of minimum element between 3 & 8 in list Mekala 2 1,640 Nov-10-2020, 12:56 PM
Last Post: DeaD_EyE
  How to calculate time difference between each row of dataframe in seconds Mekala 1 1,548 Jul-16-2020, 12:57 PM
Last Post: Larz60+
  Finding MINIMUM number in a random list is not working Mona 5 1,952 Nov-18-2019, 07:27 PM
Last Post: ThomasL
  Delete minimum occurence in a string RavCOder 10 2,455 Nov-12-2019, 01:08 PM
Last Post: RavCOder
  Minimum size Amniote 8 2,493 Jul-10-2019, 02:58 PM
Last Post: nilamo
  Correlation of Incidents using time difference Rajhesh 1 1,201 Jun-27-2019, 03:44 PM
Last Post: Larz60+
  Armstrong in minimum lines Gaurav 1 1,485 Sep-03-2018, 03:46 PM
Last Post: j.crater
  Watson Personality Insight: minimum number of words kiton 0 2,083 Apr-28-2018, 03:28 PM
Last Post: kiton
  Python find the minimum length of string to differentiate dictionary items zydjohn 3 2,805 Mar-03-2018, 05:23 PM
Last Post: Gribouillis
  Time Difference in Epoch Microseconds then convert to human readable firesh 4 9,697 Feb-27-2018, 09:08 AM
Last Post: firesh

Forum Jump:

User Panel Messages

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