Dictionary search using datetime keys

I have time series data that I now store in a dictionary where dictionary keys are datetime.datetimeobjects. Sort of:

data[datetime.datetime(2012,5,14,15,28,2)]={'error':error,'flags':flags,'value':value}

I have a question: What is the best way to find the next two times (before and after) a certain time? I need this function as quickly as possible, because it is called (~ 10000) inside a loop linearly interpolating between the two nearest points.


Currently, I have one working method that takes a ridiculously long time, because it looks through all the keys (~ 50,000):

def findTime(time):
    keys=data.keys()
    bdt=10000000000000000000
    adt=10000000000000000000
    minKey=False
    maxKey=False
    for key in keys:
        dt=(time-key).total_seconds()
        if abs(dt)<bdt and dt>0:
            bdt=abs(dt)
            minKey=key
        elif abs(dt)<adt and dt<0:
            adt=abs(dt)
            maxKey=key
    return minKey,maxKey

My attempt to use bisect:

def findTime(time):
    keys=data.keys()
    l,r = bisect.bisect_left(time,keys), bisect.bisect_right(time,keys)
    return l,r

Unfortunately, this causes an error:

TypeError: 'datetime.datetime' object does not support indexing

Any help would be appreciated.

+3
source share
3

bisect ( , , , ). keys - , .

:

def findTime(time):
    keys = sorted(data.keys())
    return bisect.bisect_left(keys, time), bisect.bisect_right(keys, time)

, , .

+3

dict.

.

1) ISO 8601 . YYYY-MM-DD. YYYY-MM-DD:HH:MM:SS. ISO 8601 , .

2) float- , , float - , HH: MM: SS. Excel Windows Unix.

1):

>>> datetime.datetime.fromtimestamp(time.time()).isoformat()
'2012-05-14T13:55:22.142548'  # a hashable, sortable dict key based on time

2):

>>> time.time()               # That is days and fraction of day since 1/1/1970 
1337028447.499273             # THAT is you dict key
>>> datetime.datetime.fromtimestamp(time.time()).timetuple()
time.struct_time(tm_year=2012, tm_mon=5, tm_mday=14, tm_hour=13, tm_min=52, tm_sec=13, tm_wday=0, tm_yday=135, tm_isdst=-1)

Python 50 000 .

datetime .

+3

, bisect, , . , , , bisect , .

Try:

keys=sorted(data.keys())
bisect.bisect_left(keys,time), bisect.bisect_right(keys,time)

In addition, you can try to optimize your code by building an object keysoutside of your function findTime. If the dictionary datadoes not change using a sequence of calls findTime, you will pay to build a sorted list only once.

+1
source

All Articles