Skip to content

read_hdf closes HDF5 stores that it didn't open. #28700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ I/O
- Bug in :func:`DataFrame.to_string` where values were truncated using display options instead of outputting the full content (:issue:`9784`)
- Bug in :meth:`DataFrame.to_json` where a datetime column label would not be written out in ISO format with ``orient="table"`` (:issue:`28130`)
- Bug in :func:`DataFrame.to_parquet` where writing to GCS would fail with `engine='fastparquet'` if the file did not already exist (:issue:`28326`)
- Bug in :func:`read_hdf` closing stores that it didn't open when Exceptions are raised (:issue:`28699`)
- Bug in :meth:`DataFrame.read_json` where using ``orient="index"`` would not maintain the order (:issue:`28557`)
- Bug in :meth:`DataFrame.to_html` where the length of the ``formatters`` argument was not verified (:issue:`28469`)

Expand Down
11 changes: 6 additions & 5 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,11 +396,12 @@ def read_hdf(path_or_buf, key=None, mode="r", **kwargs):
key = candidate_only_group._v_pathname
return store.select(key, auto_close=auto_close, **kwargs)
except (ValueError, TypeError, KeyError):
# if there is an error, close the store
try:
store.close()
except AttributeError:
pass
if not isinstance(path_or_buf, HDFStore):
# if there is an error, close the store if we opened it.
try:
store.close()
except AttributeError:
pass

raise

Expand Down
16 changes: 15 additions & 1 deletion pandas/tests/io/pytables/test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -1195,8 +1195,22 @@ def test_read_missing_key_close_store(self, setup_path):
# read with KeyError before another write
df.to_hdf(path, "k2")

def test_append_frame_column_oriented(self, setup_path):
def test_read_missing_key_opened_store(self, setup_path):
# GH 28699
with ensure_clean_path(setup_path) as path:
df = pd.DataFrame({"a": range(2), "b": range(2)})
df.to_hdf(path, "k1")

store = pd.HDFStore(path, "r")

with pytest.raises(KeyError, match="'No object named k2 in the file'"):
pd.read_hdf(store, "k2")

# Test that the file is still open after a KeyError and that we can
# still read from it.
pd.read_hdf(store, "k1")

def test_append_frame_column_oriented(self, setup_path):
with ensure_clean_store(setup_path) as store:

# column oriented
Expand Down