【问题标题】:Pandas read_hdf query by date and time rangePandas read_hdf 按日期和时间范围查询
【发布时间】:2014-10-30 03:17:59
【问题描述】:

我有一个关于如何在 pd.read_hdf 函数中过滤结果的问题。所以这是设置,我有一个 pandas 数据框(带有 np.datetime64 索引),我将它放入 hdf5 文件中。这里没有什么花哨的东西,所以没有使用层次结构或任何东西(也许我可以合并它?)。这是一个例子:

                              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))

据我所知,如果我在这里错了,请有人纠正我,但如果我使用“where”,则不会将整个原始数据集读入内存。换句话说:

这个:

pd.read_hdf('qux.h5', 'data', where='index >= 20140714 and index <= 20140715')

和这个不一样:

pd.read_hdf('qux.h5', 'data')['20140714':'20140715']

虽然最终结果完全相同,但在后台执行的操作却不同。所以我的问题是,有没有办法将该时间范围过滤器(即 .between_time())合并到我的 where 语句中?或者如果有另一种方式我应该构建我的 hdf5 文件?也许每天存储一张桌子?

谢谢!

编辑:

关于使用层次结构,我知道结构应该高度依赖于我将如何使用数据。但是,如果我们假设 I 为每个日期定义一个表(例如 'df/date_20140714'、'df/date_20140715'、...)。在这里我可能再次弄错了,但是使用我查询日期/时间范围的示例;如果我想要一个合并的输出,我需要读取每个表并且必须合并它们,因此我可能会招致性能损失?

【问题讨论】:

    标签: python pandas hdf5


    【解决方案1】:

    查看使用where mask进行选择的示例

    这是一个例子

    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()
    

    需要注意的几点。这会读入整个索引以开始。通常这不是负担。如果是,您可以直接读取它(提供启动/停止,虽然它有点手动操作这个 ATM)。当前select_column我也不相信可以接受查询。

    如果您有大量数据(数千万行,很宽),您可能会在几天内迭代(并执行单个查询),这可能会更有效。

    重组数据相对便宜(通过concat),所以不要害怕子查询(虽然这样做太多也会拖累性能)。

    【讨论】:

    • 是否可以将其与常规字符串 where 子句结合使用,例如 e.g.如果我还想通过"sym='RIC'" 过滤?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-15
    • 1970-01-01
    • 2016-02-15
    • 2019-05-26
    • 2018-04-10
    • 2017-11-18
    相关资源
    最近更新 更多