Skip to content

Commit 959a760

Browse files
kbooneWillAyd
authored andcommitted
read_hdf closes HDF5 stores that it didn't open. (#28700)
1 parent fcea0dc commit 959a760

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ I/O
311311
- Bug in :func:`DataFrame.to_string` where values were truncated using display options instead of outputting the full content (:issue:`9784`)
312312
- Bug in :meth:`DataFrame.to_json` where a datetime column label would not be written out in ISO format with ``orient="table"`` (:issue:`28130`)
313313
- Bug in :func:`DataFrame.to_parquet` where writing to GCS would fail with `engine='fastparquet'` if the file did not already exist (:issue:`28326`)
314+
- Bug in :func:`read_hdf` closing stores that it didn't open when Exceptions are raised (:issue:`28699`)
314315
- Bug in :meth:`DataFrame.read_json` where using ``orient="index"`` would not maintain the order (:issue:`28557`)
315316
- Bug in :meth:`DataFrame.to_html` where the length of the ``formatters`` argument was not verified (:issue:`28469`)
316317

pandas/io/pytables.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -396,11 +396,12 @@ def read_hdf(path_or_buf, key=None, mode="r", **kwargs):
396396
key = candidate_only_group._v_pathname
397397
return store.select(key, auto_close=auto_close, **kwargs)
398398
except (ValueError, TypeError, KeyError):
399-
# if there is an error, close the store
400-
try:
401-
store.close()
402-
except AttributeError:
403-
pass
399+
if not isinstance(path_or_buf, HDFStore):
400+
# if there is an error, close the store if we opened it.
401+
try:
402+
store.close()
403+
except AttributeError:
404+
pass
404405

405406
raise
406407

pandas/tests/io/pytables/test_store.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -1195,8 +1195,22 @@ def test_read_missing_key_close_store(self, setup_path):
11951195
# read with KeyError before another write
11961196
df.to_hdf(path, "k2")
11971197

1198-
def test_append_frame_column_oriented(self, setup_path):
1198+
def test_read_missing_key_opened_store(self, setup_path):
1199+
# GH 28699
1200+
with ensure_clean_path(setup_path) as path:
1201+
df = pd.DataFrame({"a": range(2), "b": range(2)})
1202+
df.to_hdf(path, "k1")
1203+
1204+
store = pd.HDFStore(path, "r")
11991205

1206+
with pytest.raises(KeyError, match="'No object named k2 in the file'"):
1207+
pd.read_hdf(store, "k2")
1208+
1209+
# Test that the file is still open after a KeyError and that we can
1210+
# still read from it.
1211+
pd.read_hdf(store, "k1")
1212+
1213+
def test_append_frame_column_oriented(self, setup_path):
12001214
with ensure_clean_store(setup_path) as store:
12011215

12021216
# column oriented

0 commit comments

Comments
 (0)