Skip to content

Commit df10016

Browse files
committed
Make logic that detects if there is only one dataset in a HDF5 file work when storing a dataframe that contains categorical data.
1 parent 2f41aef commit df10016

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

pandas/io/pytables.py

+25-5
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,17 @@ def read_hdf(path_or_buf, key=None, **kwargs):
331331

332332
try:
333333
if key is None:
334-
keys = store.keys()
335-
if len(keys) != 1:
336-
raise ValueError('key must be provided when HDF file contains '
337-
'multiple datasets.')
338-
key = keys[0]
334+
groups = store.groups()
335+
candidate_only_group = groups[0]
336+
# For the HDF file to have only one dataset, all other groups
337+
# should then be metadata groups for that candidate group. (This
338+
# assumes that the groups() method enumerates parent groups
339+
# before their children.)
340+
for group_to_check in groups[1:]:
341+
if not _is_metadata_of(group_to_check, candidate_only_group):
342+
raise ValueError('key must be provided when HDF file '
343+
'contains multiple datasets.')
344+
key = candidate_only_group._v_pathname
339345
return store.select(key, auto_close=auto_close, **kwargs)
340346
except:
341347
# if there is an error, close the store
@@ -347,6 +353,20 @@ def read_hdf(path_or_buf, key=None, **kwargs):
347353
raise
348354

349355

356+
def _is_metadata_of(group, parent_group):
357+
"""Check if a given group is a metadata group for a given parent_group."""
358+
if group._v_depth <= parent_group._v_depth:
359+
return False
360+
361+
current = group
362+
while current._v_depth > 1:
363+
parent = current._v_parent
364+
if parent == parent_group and current._v_name == 'meta':
365+
return True
366+
current = current._v_parent
367+
return False
368+
369+
350370
class HDFStore(StringMixin):
351371

352372
"""

0 commit comments

Comments
 (0)