【问题标题】:Invisible data in hdf5 database (with Python / Pandas / ViTables)hdf5 数据库中的不可见数据(使用 Python / Pandas / ViTables)
【发布时间】:2013-10-14 17:01:13
【问题描述】:

我有一个 hdf5 数据库,这给我带来了一些麻烦。

它应该包含 3,000 个表,其中 6 列(整数和浮点数)加上一个索引(日期)和可变数量的行(从 100 到 10,000,000)。

从昨天开始,当我使用 ViTables '查看'数据库时,我错过了数千个表。我曾经能够在 ViTables 中看到它们。但是数据仍然存在:我仍然可以通过 Pandas 访问它们。

数据整理如下:type/source/id

例如,我可以使用以下方法同时检索 id1 和 id2:

 with pd.get_store(HDF_DATABASE) as store:
     print store['type1/source1/id1']
     print store['type2/source2/id2']

但在 ViTables 中,我看不到 type2/source2/id2

此外,> print store 将列出type1/source1/id1,但不会列出type2/source2/id2

关于如何修复这些“不可见”数据表的任何建议?

编辑:

  • 错别字
  • Windows 7 32bit / Python 2.7.5 / Pandas 0.12.0(和其他 过去的版本)
  • ptdump 文件:http://pastebin.com/7mB6bT2T
  • 正如预期的那样,我混淆了类型源 ID
  • 看起来数据不再被引用,但只要数据库不是 ptrepack-ed 就仍然存在。

EDIT2:

  • 我完全丢失了原始数据库:我无法再访问它。该格式不再被识别。
  • 用于插入新数据的此语句(和其他类似语句)返回 NaturalNameWarning 警告:store.append('equity/bloomberg/4615238QCN_Equity', df)。它不尊重生成警告的自然命名要求。这可能与遇到的问题有关。

【问题讨论】:

  • 什么版本的熊猫?你昨天对文件做了什么吗?你能显示ptdump -av <filename> 并显示 id1 和 id2 的部分吗(你可能需要重定向,因为它会显示所有节点上的数据)
  • @Jeff 我使用Pandas 0.12.0,但我过去使用过其他版本(如您在 ptdump 文件中所见)。请找到转储here 的链接。如您所见,没有提及 source2 和 id2,尽管 store['type2/source2/id2'] 仍然可以正常工作。
  • 您可以发布指向您文件的链接吗?您可以尝试ptrepack in.h5 out.h5 看看新输出中是否仍然存在问题。
  • 有了ptrepack in.h5 out.h5,我无法再访问store['type2/source2/id2']out.h5 的大小大约是 in.h5 大小的一半。文件原始文件为 250GB,无法共享(我购买了数据,但无法分发)。另外,如果我删除大部分数据并将其 ptrepack 以便我可以在线共享,那么问题就不会再明显了。
  • 好的......就像在一个 HDF5 文件中存储这么多表通常不是一个好主意一样。您是否正在尝试同时编写此内容?您可能已超出元数据限制和/或以某种方式重写了组。你在桌子上做什么操作?

标签: python pandas hdf5


【解决方案1】:

示例会话

In [1]: store = pd.HDFStore('test.h5')

In [2]: store['node()'] = Series(np.arange(10))
/usr/local/lib/python2.7/site-packages/tables/path.py:99: NaturalNameWarning: object name is not a valid Python identifier: 'node()'; it does not match the pattern ``^[a-zA-Z_][a-zA-Z0-9_]*$``; you will not be able to use natural naming to access this object; using ``getattr()`` will still work, though
  NaturalNameWarning)

In [3]: store
Out[3]: 
<class 'pandas.io.pytables.HDFStore'>
File path: test.h5
/df                frame_table  (typ->appendable,nrows->11,ncols->2,indexers->[index],dc->[A,B])
/node()            series       (shape->[10])                                                   

In [4]: store.keys()
Out[4]: ['/df', '/node()']

In [5]: store['node()/foo'] = Series(np.arange(10))

In [6]: store.keys()
Out[6]: ['/df', '/node()', '/node()/foo']

In [7]: store
Out[7]: 
<class 'pandas.io.pytables.HDFStore'>
File path: test.h5
/df                    frame_table  (typ->appendable,nrows->11,ncols->2,indexers->[index],dc->[A,B])
/node()                series       (shape->[10])                                                   
/node()/foo            series       (shape->[10])                                                   

In [8]: store['my_type\mysource\id_01_01'] = Series(np.arange(10))
/usr/local/lib/python2.7/site-packages/tables/path.py:99: NaturalNameWarning: object name is not a valid Python identifier: 'my_type\\mysource\\id_01_01'; it does not match the pattern ``^[a-zA-Z_][a-zA-Z0-9_]*$``; you will not be able to use natural naming to access this object; using ``getattr()`` will still work, though
  NaturalNameWarning)

In [9]: store
Out[9]: 
<class 'pandas.io.pytables.HDFStore'>
File path: test.h5
/df                                   frame_table  (typ->appendable,nrows->11,ncols->2,indexers->[index],dc->[A,B])
/my_type\mysource\id_01_01            series       (shape->[10])                                                   
/node()                               series       (shape->[10])                                                   
/node()/foo                           series       (shape->[10])                                                   

In [10]: store.keys()
Out[10]: ['/df', '/my_type\\mysource\\id_01_01', '/node()', '/node()/foo']

In [11]: store['my_type/mysource/id_01_01'] = Series(np.arange(10))

In [12]: store
Out[12]: 
<class 'pandas.io.pytables.HDFStore'>
File path: test.h5
/df                                   frame_table  (typ->appendable,nrows->11,ncols->2,indexers->[index],dc->[A,B])
/my_type\mysource\id_01_01            series       (shape->[10])                                                   
/node()                               series       (shape->[10])                                                   
/node()/foo                           series       (shape->[10])                                                   
/my_type/mysource/id_01_01            series       (shape->[10])                                                   

问题是标识符“my_type\mysource\id_01_01”没有按照您的想法执行,它“看起来”像一个文件路径。您需要反斜杠,而不是正斜杠(因为它们取决于架构)。虽然理论上这会起作用(但为避免出现警告,您可能需要更改这些名称)。

【讨论】:

  • 我不确定这里是否理解,我需要正斜杠'/'而不是'\',这是我做的吗? (我更正了一个输入“\”而不是“/”的错字)。
  • \ 反斜杠(抱歉)是名称中的无效字符。他们不会创建分层组(如果这是您的意图)。我也不认为 PyTables 在 ptrepack 中处理它们(尽管在这一点上不确定)。我会简单地避开它们。使用/ 创建分层(树状)节点。你到底想做什么?
  • 我只在我的代码中使用了正斜杠,并且我能够在 ViTables 中很好地看到分层组。我只使用正斜杠得到了NaturalNameWarning 警告。理想情况下,我想恢复未引用的数据(最坏的情况,我有一个最新的备份)并了解出了什么问题。
  • 试图重现该问题,我注意到此语句返回 NaturalNameWarning 警告:store.append('equity/bloomberg/4615238QCN_Equity', df)。为什么?
  • AFAIK 问题以数字开头,它不是有效的标识符
猜你喜欢
  • 2014-04-26
  • 1970-01-01
  • 2016-10-27
  • 2018-02-22
  • 2017-10-27
  • 2010-12-17
  • 2020-11-06
  • 2017-02-23
  • 2017-01-21
相关资源
最近更新 更多