21

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

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'

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

4

3 回答 3

10

在我手中,以下方法效果最好:

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')
于 2015-07-08T18:17:55.530 回答
5

您可以尝试这样做:

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

或者直接使用 read 方法:

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

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

于 2014-07-07T10:10:13.860 回答
2

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

conda 安装 pytables

这让我 numexpr-2.4.3 和 pytables-3.2.0

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

于 2015-06-30T07:13:07.297 回答