Reputation: 16109
This may be a stupid question, but i have yet to find an answer in the pandas docs or elsewhere. The same question has been asked before here. But the only answer was to look at the pandas docs, which as I stated don't provide an answer to this problem.
I want to be able to build an hdf file with several datasets. Once this hdf has been closed I would like to be able to list each of the datasets contained within. For example:
import pandas as pd
import numpy as np
store = pd.HDFStore('test.h5')
df1 = pd.DataFrame(np.random.randn(10,2), columns=list('AB')
df2 = pd.DataFrame(np.random.randn(10,2), columns=list('AB')
store['df1'] = df1
store['df2'] = df2
print(store)
Returns:
<class 'pandas.io.pytables.HDFStore'>
File path: test.h5
/df1 frame (shape->[10,2])
/df2 frame (shape->[10,2])
However if you close the hdf with store.close()
and then attempt to read it using pd.read_hdf()
the following error returns:
ValueError: key must be provided when HDF contains multiple datasets.
Is there a way to return a list of all these datasets?
Thanks in advance for any help!
Upvotes: 10
Views: 3878
Reputation: 12618
Yes, there is.
store = pd.HDFStore('test.h5')
print(store)
<class 'pandas.io.pytables.HDFStore'>
File path: test.h5
/df1 frame (shape->[10,2])
/df2 frame (shape->[10,2])
Upvotes: 11