Home > Enterprise >  Find range that contains most timestamps from list in Python
Find range that contains most timestamps from list in Python

Time:09-14

I have a list of timestamps of when an event A happens, and I want to figure out if there are certain time (within the hour) when A happens.

Here is a list of times when the event happens. I formatted a new line to signify a new day. From the list we can see that the event will likely happen between 7:50 am and 8:50 am every day. The output here should be about an hour range, and since 7:50 - 8:50 is the time range that contains the most amount of events, that should be the answer.How can I figure that pattern out using Python?

I tried rounding to the nearest hour and then finding the most common time, but that gave me 7 am - 8 am instead of 7:50 am - 8:50 am. For this problem, I'm assuming event A will happen every day.

Expected answer: 7:50 - 8:50

Why? Calculating the number of timestamps one hour ranges can cover:

  1. 8:30 - 9:30: 2
  2. 14:40 - 15: 40: 1
  3. 20:40 - 21:40: 1
  4. 8:40 - 9:40: 1
  5. 20:00 - 21:00 : 2
  6. 8:40 - 9:40: 1
  7. 8:20 - 9:20: 3
  8. 7:50 - 8:50: 4
import datetime
times = [datetime.time(8, 30), datetime.time(14, 40), datetime.time(20, 40), 
         datetime.time(8, 40), datetime.time(20, 00), 
         datetime.time(8, 20), 
         datetime.time(7, 50)]

CodePudding user response:

This should work

from datetime import time, datetime, date, timedelta

def count_events(times):
    count = 0
    for t in times:
        delta = datetime.combine(date.min, t) - datetime.combine(date.min, times[0]) 
        if delta < timedelta(hours=1):
            count  = 1
        else:
            break
    return times[0], (datetime.combine(date.min, times[0]) timedelta(hours=1)).time(), count

# your events list
times = [time(8, 30), time(14, 40), time(20, 40), 
         time(8, 40), time(20, 00), 
         time(8, 20), 
         time(7, 50)]

# sort times list before counting
times.sort()

# count events in intervals
intervals = []
for index, time in enumerate(times):
    intervals.append(count_events(times[index:]))

# find the interval with max events number
most_events = max(intervals, key=lambda item:item[2])

# print results
print('\n'.join(str(e) for e in intervals))
print(f'interval with max events is: {most_events}')

The result is

(datetime.time(7, 50), datetime.time(8, 50), 4)
(datetime.time(8, 20), datetime.time(9, 20), 3)
(datetime.time(8, 30), datetime.time(9, 30), 2)
(datetime.time(8, 40), datetime.time(9, 40), 1)
(datetime.time(14, 40), datetime.time(15, 40), 1)
(datetime.time(20, 0), datetime.time(21, 0), 2)
(datetime.time(20, 40), datetime.time(21, 40), 1)

interval with max events is: (datetime.time(7, 50), datetime.time(8, 50), 4)
  • Related