@@ -331,11 +331,17 @@ def read_hdf(path_or_buf, key=None, **kwargs):
331
331
332
332
try :
333
333
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
339
345
return store .select (key , auto_close = auto_close , ** kwargs )
340
346
except :
341
347
# if there is an error, close the store
@@ -347,6 +353,20 @@ def read_hdf(path_or_buf, key=None, **kwargs):
347
353
raise
348
354
349
355
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
+
350
370
class HDFStore (StringMixin ):
351
371
352
372
"""
0 commit comments