【问题标题】:Create HDF5 file using pytables with table format and data columns使用具有表格格式和数据列的 pytables 创建 HDF5 文件
【发布时间】:2016-09-17 02:05:50
【问题描述】:

我想读取之前使用PyTables 创建的 h5 文件。

使用Pandas 读取文件,并且有一些条件,如下所示:

pd.read_hdf('myH5file.h5', 'anyTable', where='some_conditions')

从另一个问题中,有人告诉我,为了使 h5 文件可以使用 read_hdf's where 参数“可查询”,它必须写在 table format 中,此外,必须将某些列声明为 @987654327 @。

我在 PyTables 文档中找不到任何关于它的信息。

PyTable 的 create_table 方法的文档没有说明任何内容。

所以,现在,如果我尝试在使用 PyTables 创建的 h5 文件上使用类似的东西,我会得到以下信息:

>>> d = pd.read_hdf('test_file.h5','basic_data', where='operation==1')
C:\Python27\lib\site-packages\pandas\io\pytables.py:3070: IncompatibilityWarning: 
where criteria is being ignored as this version [0.0.0] is too old (or
not-defined), read the file in and write it out to a new file to upgrade (with
the copy_to method)

  warnings.warn(ws, IncompatibilityWarning)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\pandas\io\pytables.py", line 323, in read_hdf
    return f(store, True)
  File "C:\Python27\lib\site-packages\pandas\io\pytables.py", line 305, in <lambda>
    key, auto_close=auto_close, **kwargs)
  File "C:\Python27\lib\site-packages\pandas\io\pytables.py", line 665, in select
    return it.get_result()
  File "C:\Python27\lib\site-packages\pandas\io\pytables.py", line 1359, in get_result
    results = self.func(self.start, self.stop, where)
  File "C:\Python27\lib\site-packages\pandas\io\pytables.py", line 658, in func
    columns=columns, **kwargs)
  File "C:\Python27\lib\site-packages\pandas\io\pytables.py", line 3968, in read
    if not self.read_axes(where=where, **kwargs):
  File "C:\Python27\lib\site-packages\pandas\io\pytables.py", line 3196, in read_axes
    values = self.selection.select()
  File "C:\Python27\lib\site-packages\pandas\io\pytables.py", line 4482, in select
    start=self.start, stop=self.stop)
  File "C:\Python27\lib\site-packages\tables\table.py", line 1567, in read_where
    self._where(condition, condvars, start, stop, step)]
  File "C:\Python27\lib\site-packages\tables\table.py", line 1528, in _where
    compiled = self._compile_condition(condition, condvars)
  File "C:\Python27\lib\site-packages\tables\table.py", line 1366, in _compile_condition
    compiled = compile_condition(condition, typemap, indexedcols)
  File "C:\Python27\lib\site-packages\tables\conditions.py", line 430, in compile_condition
    raise _unsupported_operation_error(nie)
NotImplementedError: unsupported operand types for *eq*: int, bytes

编辑:

回溯提到了一些关于 IncompatibilityWarning 和版本 [0.0.0] 的内容,但是如果我检查我的 Pandas 和 Tables 版本,我会得到:

>>> import pandas
>>> pandas.__version__
'0.15.2'
>>> import tables
>>> tables.__version__
'3.1.1'

所以,我完全糊涂了。

【问题讨论】:

  • 好吧,你正在使用 pandas 来读取这个文件,所以文档是 here。 Pandas 可以读取 PyTables Table 格式。所以并不完全清楚你是如何创造事物的。
  • 杰夫,问题不在于阅读,而在于如何使用 PyTables 创建 h5 文件并使表格具有 tableformat 并使某些列成为 data columns 以便能够阅读它们Pandas,据我所知,此信息不在 PyTables 文档中
  • 你必须使用 pandas 创建它们:pytables 只是一个存储层。请阅读我指出的文档

标签: python pandas hdf5 pytables


【解决方案1】:

我遇到了同样的问题,这就是我所做的。

  1. 通过 PyTables 创建 HDF5 文件;
  2. 通过 pandas.read_hdf 读取此 HDF5 文件并使用“where = where_string, columns = selected_columns”等参数

  3. 我收到如下警告消息和其他错误消息:

    D:\程序 文件\Anaconda3\lib\site-packages\pandas\io\pytables.py:3065: IncompatibilityWarning:标准被忽略的地方 版本 [0.0.0] 太旧(或未定义),读取文件并 将其写入新文件以进行升级(使用 copy_to 方法)

    warnings.warn(ws, IncompatibilityWarning)

  4. 我试过这样的命令:

    hdf5_store = pd.HDFStore(hdf5_file, mode = 'r')

    h5cpt_store_new = hdf5_store.copy(hdf5_new_file, complevel=9, complib='blosc') h5cpt_store_new.close()

  5. 并像第 2 步一样运行命令,它可以工作。

    熊猫。版本 '0.17.1'

    表格。版本 '3.2.2'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-25
    • 2011-12-21
    • 2013-01-21
    • 2015-05-18
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    • 1970-01-01
    相关资源
    最近更新 更多