Repository: pandas-dev/pandas
Pull request: ENH: clearer error message if read_hdf loads upsupported HDF file (GH9539)
- [x] closes #9539
- [x] tests added / passed
- [x] passes `black pandas`
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [x] whatsnew entry

The error message changed since the original bug report but still could be made more clear.

Problem statement:
Unclear error when loading unsupported HDF5 file
Backstory - I created some H5 files with h5py containing large 3D arrays (not python dataframes). Then I forgot this and tried to load with Pandas. Since they didn't contain real dataframes the load understandably  failed. However the error thrown was:

`TypeError: cannot create a storer if the object is not existing nor a value are passed`

(line 1148 in pandas.io.pytables) which apart from being awkward grammar (rather `cannot create a storer if the object does not exist nor a value given`) doesn't suggest anything about the h5 file not being supported.

Since passing around h5 files is fairly common and sometimes they contain tables and sometimes not, it would be nice if Pandas warned you clearly that the object you are trying to load isn't a pandas-compatible table.

Hints from issue discussion:
certainly willing to take a PR to make the error message more clear.

Unfortuntately reading `h5py` is pretty out of scope as its a non-easily interpretable format and doesn't have much meta data.

That said, you can use [into](https://github.com/ContinuumIO/into) to do this, something like

`into('hdfstore:://file.hdf','h5py:://file.h5')` will do the conversion (only will work with a 1/2-D array-like though)

I agree there's no need to read h5py directly, just a more clear error. Will have a look if I get time...

-----Original Message-----
From: "jreback" notifications@github.com
Sent: ‎24/‎02/‎2015 21:26
To: "pydata/pandas" pandas@noreply.github.com
Cc: "mangecoeur" jon.chambers3001@gmail.com
Subject: Re: [pandas] Unclear error when loading unsupported HDF5 file (#9539)

certainly willing to take a PR to make the error message more clear.
Unfortuntately reading h5py is pretty out of scope as its a non-easily interpretable format and doesn't have much meta data.
That said, you can use into to do this, something like
into('hdfstore:file.hdf','h5py:file.h5') will do the conversion
—
Reply to this email directly or view it on GitHub.

Commit message: ENH: clearer error message if read_hdf loads upsupported HDF file (GH9539)

Patch to review:
diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst
index 5fe7d12188860..c126498f67539 100644
--- a/doc/source/whatsnew/v1.1.0.rst
+++ b/doc/source/whatsnew/v1.1.0.rst
@@ -98,6 +98,8 @@ Other enhancements
   This can be used to set a custom compression level, e.g.,
   ``df.to_csv(path, compression={'method': 'gzip', 'compresslevel': 1}``
   (:issue:`33196`)
+- :func:`pandas.read_hdf` has a more explicit error message when loading an
+  unsupported HDF file (:issue:`9539`)
 
 .. ---------------------------------------------------------------------------
 
diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py
index 3dd87ae6ed758..be943349c18d3 100644
--- a/pandas/io/pytables.py
+++ b/pandas/io/pytables.py
@@ -387,7 +387,10 @@ def read_hdf(
         if key is None:
             groups = store.groups()
             if len(groups) == 0:
-                raise ValueError("No dataset in HDF5 file.")
+                raise ValueError(
+                    "Dataset that is either compatible with Pandas data types "
+                    "or is a table could not be found in HDF5 file."
+                )
             candidate_only_group = groups[0]
 
             # For the HDF file to have only one dataset, all other groups


Code review comments:
diff_hunk: @@ -387,7 +387,10 @@ def read_hdf(
         if key is None:
             groups = store.groups()
             if len(groups) == 0:
-                raise ValueError("No dataset in HDF5 file.")
+                raise ValueError(
+                    "Dataset that is either compatible with Pandas data types "
path: pandas/io/pytables.py
text: @user1:
incompatible

@author:
Updated. Is that better?