[Python] How do you efficiently find gaps in one list of datetimes relative to another?

Discussão em 'Python' iniciado por Stack, Abril 26, 2025.

  1. Stack

    Stack Membro Participativo

    I have datasets from multiple instruments with differing, but hypothetically concurrent datetime stamps. If date from instrument A does not correspond to any data from instrument B within some interval, I want to flag those data for removal later.

    The current way I handle this is to loop over A and test against B with a list comprehension. However, my datasets can get very large -- O(400k) -- which makes this looping on loops extremely slow and inefficient.

    How can I reformulate the following "findGaps" method to be more efficient?

    from datetime import datetime, timedelta
    import time

    start = datetime(2025,4,25,0,0,0)
    stop = datetime(2025,4,25,2,0,0)
    delta = timedelta(seconds=1)

    # Main dataset:
    dateTimeMain = []
    while start <= stop:
    start += delta
    dateTimeMain.append(start)

    # Test dataset with gaps:
    dateTimeTest = dateTimeMain.copy()
    del dateTimeTest[30:300]
    del dateTimeTest[3000:3300]

    def findGaps(dTM,dTT):
    bTs = []
    start = -1
    i, index, stop = 0,0,0
    tThreshold = timedelta(seconds=30)

    for index, esTimeI in enumerate(dTM):

    tDiff = [abs(x - esTimeI) for x in dTT]
    if min(tDiff) > tThreshold:
    i += 1
    if start == -1:
    start = index
    stop = index
    else:
    if start != -1:
    startstop = [dTM[start],dTM[stop]]
    msg = f' Flag data from {startstop[0]} to {startstop[1]}'
    print(msg)
    bTs.append(startstop)
    start = -1

    if start != -1 and stop == index: # Records from a mid-point to the end are bad
    startstop = [dTM[start],dTM[stop]]
    bTs.append(startstop)

    return bTs

    tic = time.process_time()
    badTimes = findGaps(dateTimeMain,dateTimeTest)
    print(f'Loops: {len(dateTimeMain)} x {len(dateTimeTest)} = {len(dateTimeMain)*len(dateTimeTest)}')
    print(f'Uncertainty Update Elapsed Time: {time.process_time() - tic:.3f} s')


    Returns:

    Flag data from 2025-04-25 00:01:01 to 2025-04-25 00:04:30
    Flag data from 2025-04-25 00:55:01 to 2025-04-25 00:59:00
    Loops: 7201 x 6631 = 47749831
    Uncertainty Update Elapsed Time: 5.167 s

    Continue reading...

Compartilhe esta Página