我在HDFStore有熊猫的数据,索引作为划时代的时间存储。我想根据特定的索引读取数据和查询。
例如,如果我在datetime64ns中有索引而不是epoc,我可以获得如下结果:
starttime = datetime.datetime(2008,12,22,00,19,55,150000)
start = pd.Timestamp(stoptime) + pd.Timedelta(1)
stoptime = datetime.datetime(2008,12,22,00,55,55,180000)
stop = pd.Timestamp(starttime) + pd.Timedelta(1)
pd.read_hdf('file.h5',columns=['Data','Qty'],where='index > start & index < stop']如果将HDFStore中的索引存储为划时代的时间,如何实现相同的结果?
发布于 2016-12-29 10:31:13
IIUC,您应该将您的start和stop时间转换为纪元,以便能够执行查询。要做到这一点,你可以:
start_epoch = (start - datetime.datetime(1970,1,1)).total_seconds()
stop_epoch = (stop - datetime.datetime(1970,1,1)).total_seconds()然后,您应该能够对它们执行查询。使用您的数据:
In [24]: (start - datetime.datetime(1970,1,1)).total_seconds()
Out[24]: 1229905195.15
In [27]: (stop - datetime.datetime(1970,1,1)).total_seconds()
Out[27]: 1229907355.18编辑:更一般的问题。对于numpy datetime64对象,您可以首先将其转换为普通datetime。
In [16]: abc = np.datetime64('2005-12-27 20:10:10.500400300', 'ns')
In [17]: a = pd.to_datetime(abc)
In [18]: a
Out[18]: Timestamp('2005-12-27 20:10:10.500400300')然后您可以使用上述方法。
https://stackoverflow.com/questions/41377097
复制相似问题