From 73d4f85a17b8a108c63d019aed86a77a6319ce37 Mon Sep 17 00:00:00 2001 From: Kyle Boone Date: Mon, 30 Sep 2019 17:34:37 -0700 Subject: [PATCH 1/4] BUG: Fix read_hdf closing store that it didn't open (GH28699) --- pandas/io/pytables.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index 55ccd838f8a16..0db5b1b4eecfa 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -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 From 639e3652bee5a88c17f760e97682114382548820 Mon Sep 17 00:00:00 2001 From: Kyle Boone Date: Mon, 30 Sep 2019 17:36:27 -0700 Subject: [PATCH 2/4] TST: Add test for read_hdf store closing bug (GH28699) --- pandas/tests/io/pytables/test_pytables.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/pandas/tests/io/pytables/test_pytables.py b/pandas/tests/io/pytables/test_pytables.py index 46d8ef04dd8e5..cf402bed14e85 100644 --- a/pandas/tests/io/pytables/test_pytables.py +++ b/pandas/tests/io/pytables/test_pytables.py @@ -1294,8 +1294,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 From 7e4d242b84c8a58a175d4574e1ec38e39be00608 Mon Sep 17 00:00:00 2001 From: Kyle Boone Date: Wed, 2 Oct 2019 14:37:47 -0700 Subject: [PATCH 3/4] Change quote style --- pandas/tests/io/pytables/test_pytables.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/io/pytables/test_pytables.py b/pandas/tests/io/pytables/test_pytables.py index cf402bed14e85..9f458aa336917 100644 --- a/pandas/tests/io/pytables/test_pytables.py +++ b/pandas/tests/io/pytables/test_pytables.py @@ -1300,7 +1300,7 @@ def test_read_missing_key_opened_store(self, setup_path): df = pd.DataFrame({"a": range(2), "b": range(2)}) df.to_hdf(path, "k1") - store = pd.HDFStore(path, 'r') + store = pd.HDFStore(path, "r") with pytest.raises(KeyError, match="'No object named k2 in the file'"): pd.read_hdf(store, "k2") From b87f11fbccdaa7a5ef6c411b9d1264955ff86b90 Mon Sep 17 00:00:00 2001 From: Kyle Boone Date: Thu, 3 Oct 2019 10:34:42 -0700 Subject: [PATCH 4/4] Add what's new for GH28699 --- doc/source/whatsnew/v1.0.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index 2668734031ee1..17709be903ea7 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -260,6 +260,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`) Plotting ^^^^^^^^