【问题标题】:pandas HDFStore - how to reopen?pandas HDFStore - 如何重新打开?
【发布时间】:2015-07-09 02:17:55
【问题描述】:

我使用以下方法创建了一个文件:

store = pd.HDFStore('/home/.../data.h5')

并使用以下方法存储了一些表:

store['firstSet'] = df1
store.close()

我关闭了 python 并在一个新的环境中重新打开。

如何重新打开此文件?

我去的时候:

store = pd.HDFStore('/home/.../data.h5')

我收到以下错误。

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/misc/apps/linux/python-2.6.1/lib/python2.6/site-packages/pandas-0.10.0-py2.6-linux-x86_64.egg/pandas/io/pytables.py", line 207, in __init__
    self.open(mode=mode, warn=False)
  File "/misc/apps/linux/python-2.6.1/lib/python2.6/site-packages/pandas-0.10.0-py2.6-linux-x86_64.egg/pandas/io/pytables.py", line 302, in open
    self.handle = _tables().openFile(self.path, self.mode)
  File "/apps/linux/python-2.6.1/lib/python2.6/site-packages/tables/file.py", line 230, in openFile
    return File(filename, mode, title, rootUEP, filters, **kwargs)
  File "/apps/linux/python-2.6.1/lib/python2.6/site-packages/tables/file.py", line 495, in __init__
    self._g_new(filename, mode, **params)
  File "hdf5Extension.pyx", line 317, in tables.hdf5Extension.File._g_new (tables/hdf5Extension.c:3039)
tables.exceptions.HDF5ExtError: HDF5 error back trace

  File "H5F.c", line 1582, in H5Fopen
    unable to open file
  File "H5F.c", line 1373, in H5F_open
    unable to read superblock
  File "H5Fsuper.c", line 334, in H5F_super_read
    unable to find file signature
  File "H5Fsuper.c", line 155, in H5F_locate_signature
    unable to find a valid file signature

End of HDF5 error back trace

Unable to open/create file '/home/.../data.h5'

我在这里做错了什么?谢谢。

【问题讨论】:

  • 你能尝试升级0.10.1吗,我认为在0.10.0和0.1之间有一些注意事项。 (这似乎在我的系统上毫无例外地工作。)
  • 会的。最初创建文件和将来访问它有区别吗?还是 pd.HDFStore 做同样的事情(如果不存在则创建,如果存在则打开?)
  • 不,它应该完全按照你说的那样工作,所以你的代码应该工作:)。我认为如果您的 HDF5 安装出现问题,它会在此之前生闷气,除了更新之外想不到它会是什么!
  • 太棒了。我正在更新中。谢谢。
  • Andy 是对的,看起来像 HDF5 问题。如果更新确实有效,请发布表格版本、您的操作系统,然后运行 ​​'ptdump -av '

标签: python pandas


【解决方案1】:

在我看来,以下方法效果最好:

df = pd.DataFrame(...)

"write"
with pd.HDFStore('test.h5',  mode='w') as store:
    store.append('df', df, data_columns= df.columns, format='table')

"read"
with pd.HDFStore('test.h5',  mode='r') as newstore:
    df_restored = newstore.select('df')

【讨论】:

  • 但在撰写此评论时,具有“w”模式的 pd.HDFStore 会重新创建一个新文件。
【解决方案2】:

您可以尝试这样做:

store = pd.io.pytables.HDFStore('/home/.../data.h5')
df1 = store['firstSet']

或者直接使用read方法:

df1 = pd.read_hdf('/home/.../data.h5', 'firstSet')

无论哪种方式,您都应该拥有 pandas 0.12.0 或更高版本...

【讨论】:

    【解决方案3】:

    我遇到了同样的问题,最后通过安装 pytables 模块(在我使用的 pandas 模块旁边)解决了这个问题:

    conda 安装 pytables

    这让我得到了 numexpr-2.4.3 和 pytables-3.2.0

    之后它起作用了。我在 python 2.7.9 下使用 pandas 0.16.2

    【讨论】:

      猜你喜欢
      • 2018-07-27
      • 2017-06-12
      • 1970-01-01
      • 2013-04-03
      • 2012-12-30
      • 2019-10-17
      • 2016-06-19
      • 1970-01-01
      • 2016-10-05
      相关资源
      最近更新 更多