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
  Is there a difference between Python’s time.sleep and win32api.Sleep? phpjunkie 4 1,037 Sep-21-2024, 05:17 PM
Last Post: aakritiintelligence
  time difference bettwenn logs enkliy 14 3,350 Nov-21-2023, 04:51 PM
Last Post: rob101
  Hard time trying to figure out the difference between two strings carecavoador 2 1,378 Aug-16-2023, 04:53 PM
Last Post: carecavoador
  Sum up Time difference tester_V 10 6,848 Apr-06-2023, 06:54 AM
Last Post: Gribouillis
  "TypeError: string indices must be integers, not 'str'" while not using any indices bul1t 2 5,661 Feb-11-2023, 07:03 PM
Last Post: deanhystad
  How to get index of minimum element between 3 & 8 in list Mekala 2 3,173 Nov-10-2020, 12:56 PM
Last Post: DeaD_EyE
  How to calculate time difference between each row of dataframe in seconds Mekala 1 3,545 Jul-16-2020, 12:57 PM
Last Post: Larz60+
  Finding MINIMUM number in a random list is not working Mona 5 4,343 Nov-18-2019, 07:27 PM
Last Post: ThomasL
  Delete minimum occurence in a string RavCOder 10 5,731 Nov-12-2019, 01:08 PM
Last Post: RavCOder
  Minimum size Amniote 8 5,309 Jul-10-2019, 02:58 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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