python - Pandas read_hdf 按日期和时间范围查询

标签 python pandas hdf5

我有一个关于如何在 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 文件?也许每天存储一张 table ?

谢谢!

编辑:

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

最佳答案

查看使用 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()

有几点需要注意。这将读入整个索引以开始。通常这不是负担。如果是的话,你可以只读它的 block (提供开始/停止,虽然它有点手动来做这​​个 ATM)。我认为当前的 select_column 也不能接受查询。

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

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

关于python - Pandas read_hdf 按日期和时间范围查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25681308/

相关文章:

python - 如何在别人的 Windows 机器上的虚拟环境中本地运行 python 脚本并使其始终工作?

python - 如何使用 h5py 将数据附加到 hdf5 文件中的一个特定数据集

python - 使用 Pandas 将多列取消列为行

python - 将字符串数组(类别)从 Pandas 数据帧转换为整数数组

python-3.x - Pandas - 使用 to_hdf 添加同名数据框使文件大小翻倍

python - 尝试在 h5py : ValueError: Unable to set extend dataset (Dimension cannot exceed the existing maximal size 中扩展现有数据集时出错

python - 打印列表的制表符分隔值

python - 从 django.core.mail EmailMessage 进入死循环

python - 使用 BeautifulSoup 搜索具有多个空格和通配符的类标签

python追加错误索引1超出了尺寸为1的轴0的范围