Python Pandas read_hdf按日期和时间范围查询
我有一个关于如何在pd.read_hdf函数中过滤结果的问题。这里是设置,我有一个pandas数据帧(带有np.datetime64索引),我把它放在一个hdf5文件中。这里没有什么新奇的东西,所以没有层次结构或任何东西(也许我可以合并它?)。下面是一个例子:Python Pandas read_hdf按日期和时间范围查询,python,pandas,hdf5,Python,Pandas,Hdf5,我有一个关于如何在pd.read_hdf函数中过滤结果的问题。这里是设置,我有一个pandas数据帧(带有np.datetime64索引),我把它放在一个hdf5文件中。这里没有什么新奇的东西,所以没有层次结构或任何东西(也许我可以合并它?)。下面是一个例子: Foo Bar TIME 2014-07-14 12:02:00
Foo Bar
TIME
2014-07-14 12:02:00 0 0
2014-07-14 12:03:00 0 0
2014-07-14 12:04:00 0 0
2014-07-14 12:05:00 0 0
2014-07-14 12:06:00 0 0
2014-07-15 12:02:00 0 0
2014-07-15 12:03:00 0 0
2014-07-15 12:04:00 0 0
2014-07-15 12:05:00 0 0
2014-07-15 12:06:00 0 0
2014-07-16 12:02:00 0 0
2014-07-16 12:03:00 0 0
2014-07-16 12:04:00 0 0
2014-07-16 12:05:00 0 0
2014-07-16 12:06:00 0 0
现在,我使用以下命令将其存储到.h5中:
store = pd.HDFStore('qux.h5')
#generate df
store.append('data', df)
store.close()
下一步,我将有另一个进程访问此数据,我希望获取此数据的日期/时间切片。因此,假设我希望日期介于2014-07-14和2014-07-15之间,并且只在12:02:00和12:04:00之间。目前,我正在使用以下命令检索此文件:
pd.read_hdf('qux.h5', 'data', where='index >= 20140714 and index <= 20140715').between_time(start_time=datetime.time(12,2), end_time=datetime.time(12,4))
虽然最终结果完全相同,但在后台所做的事情却不一样。所以我的问题是,有没有一种方法可以将时间范围过滤器(即between_time())合并到where语句中?或者,如果有其他方法,我应该结构我的hdf5文件?也许每天都要准备一张桌子
谢谢
编辑:
关于使用层次结构,我知道结构应该高度依赖于我将如何使用数据。但是,如果我们假设I定义了每个日期的表(例如“df/date_20140714”、“df/date_20140715”、…)。在这里我可能又错了,但使用我查询日期/时间范围的示例;我可能会受到性能方面的影响,因为如果我想要合并输出,我需要读取每个表,并且必须合并它们。请参阅使用 这里有一个例子
In [50]: pd.set_option('max_rows',10)
In [51]: df = DataFrame(np.random.randn(1000,2),index=date_range('20130101',periods=1000,freq='H'))
In [52]: df
Out[52]:
0 1
2013-01-01 00:00:00 -0.467844 1.038375
2013-01-01 01:00:00 0.057419 0.914379
2013-01-01 02:00:00 -1.378131 0.187081
2013-01-01 03:00:00 0.398765 -0.122692
2013-01-01 04:00:00 0.847332 0.967856
... ... ...
2013-02-11 11:00:00 0.554420 0.777484
2013-02-11 12:00:00 -0.558041 1.833465
2013-02-11 13:00:00 -0.786312 0.501893
2013-02-11 14:00:00 -0.280538 0.680498
2013-02-11 15:00:00 1.533521 -1.992070
[1000 rows x 2 columns]
In [53]: store = pd.HDFStore('test.h5',mode='w')
In [54]: store.append('df',df)
In [55]: c = store.select_column('df','index')
In [56]: where = pd.DatetimeIndex(c).indexer_between_time('12:30','4:00')
In [57]: store.select('df',where=where)
Out[57]:
0 1
2013-01-01 00:00:00 -0.467844 1.038375
2013-01-01 01:00:00 0.057419 0.914379
2013-01-01 02:00:00 -1.378131 0.187081
2013-01-01 03:00:00 0.398765 -0.122692
2013-01-01 04:00:00 0.847332 0.967856
... ... ...
2013-02-11 03:00:00 0.902023 1.416775
2013-02-11 04:00:00 -1.455099 -0.766558
2013-02-11 13:00:00 -0.786312 0.501893
2013-02-11 14:00:00 -0.280538 0.680498
2013-02-11 15:00:00 1.533521 -1.992070
[664 rows x 2 columns]
In [58]: store.close()
有几点需要注意。这将读取整个索引以开始。通常这不是负担。如果是的话,你可以直接读它(提供启动/停止,尽管这有点像手册)。当前select\u列
我认为您也不能接受查询
如果您有大量数据(数千万行,很宽),那么您可以在几天内进行迭代(并执行单独的查询),这可能会更有效
重新编译数据相对便宜(通过
concat
),所以不要害怕子查询(尽管这样做太多也会拖累性能)。是否可以将其与常规字符串where
子句组合,例如,如果我还想按“sym='RIC'
进行过滤?
pd.read_hdf('qux.h5', 'data')['20140714':'20140715']
In [50]: pd.set_option('max_rows',10)
In [51]: df = DataFrame(np.random.randn(1000,2),index=date_range('20130101',periods=1000,freq='H'))
In [52]: df
Out[52]:
0 1
2013-01-01 00:00:00 -0.467844 1.038375
2013-01-01 01:00:00 0.057419 0.914379
2013-01-01 02:00:00 -1.378131 0.187081
2013-01-01 03:00:00 0.398765 -0.122692
2013-01-01 04:00:00 0.847332 0.967856
... ... ...
2013-02-11 11:00:00 0.554420 0.777484
2013-02-11 12:00:00 -0.558041 1.833465
2013-02-11 13:00:00 -0.786312 0.501893
2013-02-11 14:00:00 -0.280538 0.680498
2013-02-11 15:00:00 1.533521 -1.992070
[1000 rows x 2 columns]
In [53]: store = pd.HDFStore('test.h5',mode='w')
In [54]: store.append('df',df)
In [55]: c = store.select_column('df','index')
In [56]: where = pd.DatetimeIndex(c).indexer_between_time('12:30','4:00')
In [57]: store.select('df',where=where)
Out[57]:
0 1
2013-01-01 00:00:00 -0.467844 1.038375
2013-01-01 01:00:00 0.057419 0.914379
2013-01-01 02:00:00 -1.378131 0.187081
2013-01-01 03:00:00 0.398765 -0.122692
2013-01-01 04:00:00 0.847332 0.967856
... ... ...
2013-02-11 03:00:00 0.902023 1.416775
2013-02-11 04:00:00 -1.455099 -0.766558
2013-02-11 13:00:00 -0.786312 0.501893
2013-02-11 14:00:00 -0.280538 0.680498
2013-02-11 15:00:00 1.533521 -1.992070
[664 rows x 2 columns]
In [58]: store.close()