Skip to content

Commit e839638

Browse files
committed
Raise a better exception when the HDF file is empty and kwy=None.
1 parent 611aa28 commit e839638

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

pandas/io/pytables.py

+2
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,8 @@ def read_hdf(path_or_buf, key=None, **kwargs):
332332
try:
333333
if key is None:
334334
groups = store.groups()
335+
if len(groups) == 0:
336+
raise ValueError('No dataset in HDF file.')
335337
candidate_only_group = groups[0]
336338

337339
# For the HDF file to have only one dataset, all other groups

pandas/io/tests/test_pytables.py

+6
Original file line numberDiff line numberDiff line change
@@ -4899,6 +4899,12 @@ def test_read_nokey_table(self):
48994899
df.to_hdf(path, 'df2', mode='a', format='table')
49004900
self.assertRaises(ValueError, read_hdf, path)
49014901

4902+
def test_read_nokey_empty(self):
4903+
with ensure_clean_path(self.path) as path:
4904+
store = HDFStore(path)
4905+
store.close()
4906+
self.assertRaises(ValueError, read_hdf, path)
4907+
49024908
def test_read_from_pathlib_path(self):
49034909

49044910
# GH11773

0 commit comments

Comments
 (0)