pandas HDFStore - 如何重新打开?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14591855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 20:37:46  来源:igfitidea点击:

pandas HDFStore - how to reopen?

pythonpandas

提问by user1911092

I created a file by using:

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

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

and stored some tables using:

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

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

I closed down python and reopened in a fresh environment.

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

How do I reopen this file?

如何重新打开此文件?

When I go:

我去的时候:

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

I get the following error.

我收到以下错误。

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'

What am I doing wrong here? Thank you.

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

回答by Dima Lituiev

In my hands, following approach works best:

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

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')

回答by paulo.filip3

You could try doing instead:

你可以尝试做:

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

or use the read method directly:

或者直接使用read方法:

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

Either way, you should have pandas 0.12.0 or higher...

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

回答by Eelco van Vliet

I had the same problem and finally fixed it by installing the pytables module (next to the pandas modules which I was using):

我遇到了同样的问题,最后通过安装 pytables 模块(在我使用的 Pandas 模块旁边)修复了它:

conda install pytables

conda 安装 pytables

which got me numexpr-2.4.3 and pytables-3.2.0

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

After that it worked. I am using pandas 0.16.2 under python 2.7.9

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