Pandas cannot read hdf5 file created using h5py

I get a pandas error when I try to read HDF5 files created by me using h5py. Interesting, am I just doing something wrong?

import h5py
import numpy as np
import pandas as pd
h5_file = h5py.File('test.h5', 'w')
h5_file.create_dataset('zeros', data=np.zeros(shape=(3, 5)), dtype='f')
h5_file.close()
pd_file = pd.read_hdf('test.h5', 'zeros')

gives an error: TypeError: cannot create storage if the object does not exist or the value is not passed

I tried to specify the key set to '/ zeros' (as I would do with h5py when reading the file) with no luck.

If I use pandas.HDFStore to read it, I get empty storage back:

store = pd.HDFStore('test.h5')
>>> store
<class 'pandas.io.pytables.HDFStore'>
File path: test.h5
Empty

I have no problem reading the newly created file with h5py:

h5_back = h5py.File('test.h5', 'r')
h5_back['/zeros']
<HDF5 dataset "zeros": shape (3, 5), type "<f4">

Using these versions:

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 23 2015, 02:52:03) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin

pd.__version__
'0.16.2'
h5py.__version__
'2.5.0'

Thanks a lot in advance, Masha

+11
source share
1 answer

pytables pandas.io , pandas HDF , pandas. , ,

import pandas as pd
import numpy as np
pd.Series(np.zeros((3,5),dtype=np.float32).to_hdf('test.h5','test')

'test.h5' HDFView, /test 4 , DataFrame.

HDFView of test.h5

, , NumPy - , pandas.

+9

Source: https://habr.com/ru/post/1660117/


All Articles