Skip to content

Commit 546a7b5

Browse files
a-y-khanrhshadrach
authored andcommitted
ENH: clearer error message if read_hdf loads upsupported HDF file (GH9539) (pandas-dev#33509)
* ENH: clearer error message if read_hdf loads upsupported HDF file (GH9539) * ENH: less convoluted error message (GH9539) * ENH: adding file with an incompatible dataset based on PR feedback and a test (GH9539) * Move note to io section as requested in PR feedback.
1 parent fc47e1d commit 546a7b5

File tree

4 files changed

+17
-1
lines changed

4 files changed

+17
-1
lines changed

doc/source/whatsnew/v1.1.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,8 @@ I/O
576576
- Bug in :meth:`read_excel` did not correctly handle multiple embedded spaces in OpenDocument text cells. (:issue:`32207`)
577577
- Bug in :meth:`read_json` was raising ``TypeError`` when reading a list of booleans into a Series. (:issue:`31464`)
578578
- Bug in :func:`pandas.io.json.json_normalize` where location specified by `record_path` doesn't point to an array. (:issue:`26284`)
579+
- :func:`pandas.read_hdf` has a more explicit error message when loading an
580+
unsupported HDF file (:issue:`9539`)
579581

580582
Plotting
581583
^^^^^^^^

pandas/io/pytables.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,10 @@ def read_hdf(
387387
if key is None:
388388
groups = store.groups()
389389
if len(groups) == 0:
390-
raise ValueError("No dataset in HDF5 file.")
390+
raise ValueError(
391+
"Dataset(s) incompatible with Pandas data types, "
392+
"not table, or no datasets found in HDF5 file."
393+
)
391394
candidate_only_group = groups[0]
392395

393396
# For the HDF file to have only one dataset, all other groups
Binary file not shown.

pandas/tests/io/pytables/test_store.py

+11
Original file line numberDiff line numberDiff line change
@@ -4776,3 +4776,14 @@ def test_to_hdf_multiindex_extension_dtype(self, idx, setup_path):
47764776
with ensure_clean_path(setup_path) as path:
47774777
with pytest.raises(NotImplementedError, match="Saving a MultiIndex"):
47784778
df.to_hdf(path, "df")
4779+
4780+
def test_unsuppored_hdf_file_error(self, datapath):
4781+
# GH 9539
4782+
data_path = datapath("io", "data", "legacy_hdf/incompatible_dataset.h5")
4783+
message = (
4784+
r"Dataset\(s\) incompatible with Pandas data types, "
4785+
"not table, or no datasets found in HDF5 file."
4786+
)
4787+
4788+
with pytest.raises(ValueError, match=message):
4789+
pd.read_hdf(data_path)

0 commit comments

Comments
 (0)