Repository: pandas-dev/pandas

Issue:
read_hdf closes HDF5 stores that it didn't open.
#### Code Sample, a copy-pastable example if possible

```python
import pandas as pd
df = pd.DataFrame({'a': range(10), 'b': range(10)})

df.to_hdf('/tmp/1.hdf', 'key2')

store = pd.HDFStore('/tmp/1.hdf', 'r')

try:
    pd.read_hdf(store, 'key1')
except KeyError:
    pass

pd.read_hdf(store, 'key2')
```

#### Problem description

Due to a recent change in pandas, the read_hdf function closes HDF5 stores if it fails to read from the file. In my opinion, this is the expected behavior if read_hdf opened the file itself, but shouldn't happen if it was passed a file that is already open. One use case for this is a series of try/except blocks that try to read a series of different keys from an HDF5 file that may or may not have the individual keys, as in the example above. The attempt to read key2 will fail because the first attempt will have closed the file.

This issue was introduced in pull #25863 to address issue #25766 where read_hdf wasn't closing files that it hadn't opened itself.

The fix is pretty easy, just add `if not isinstance(path_or_buf, HDFStore):` before `store.close()` at the end of read_hdf().

#### Expected Output

Should not raise an Exception when reading key2.

#### Output of ``pd.show_versions()``

<details>

[paste the output of ``pd.show_versions()`` here below this line]

INSTALLED VERSIONS
------------------
commit           : None
python           : 3.7.4.final.0
python-bits      : 64
OS               : Linux
OS-release       : 3.10.0-957.27.2.el7.x86_64
machine          : x86_64
processor        : x86_64
byteorder        : little
LC_ALL           : en_US.utf8
LANG             : en_US.utf8
LOCALE           : en_US.UTF-8

pandas           : 0.25.1
numpy            : 1.17.2
pytz             : 2019.2
dateutil         : 2.8.0
pip              : 19.2.3
setuptools       : 41.0.1
Cython           : 0.29.13
pytest           : 5.1.2
hypothesis       : None
sphinx           : 2.2.0
blosc            : None
feather          : None
xlsxwriter       : None
lxml.etree       : None
html5lib         : None
pymysql          : None
psycopg2         : None
jinja2           : 2.10.1
IPython          : 7.8.0
pandas_datareader: None
bs4              : 4.8.0
bottleneck       : None
fastparquet      : None
gcsfs            : None
lxml.etree       : None
matplotlib       : 3.1.1
numexpr          : 2.7.0
odfpy            : None
openpyxl         : None
pandas_gbq       : None
pyarrow          : None
pytables         : None
s3fs             : None
scipy            : 1.3.1
sqlalchemy       : None
tables           : 3.5.2
xarray           : None
xlrd             : None
xlwt             : None
xlsxwriter       : None


</details>

read_hdf closes HDF5 stores that it didn't open.
#### Code Sample, a copy-pastable example if possible

```python
import pandas as pd
df = pd.DataFrame({'a': range(10), 'b': range(10)})

df.to_hdf('/tmp/1.hdf', 'key2')

store = pd.HDFStore('/tmp/1.hdf', 'r')

try:
    pd.read_hdf(store, 'key1')
except KeyError:
    pass

pd.read_hdf(store, 'key2')
```

#### Problem description

Due to a recent change in pandas, the read_hdf function closes HDF5 stores if it fails to read from the file. In my opinion, this is the expected behavior if read_hdf opened the file itself, but shouldn't happen if it was passed a file that is already open. One use case for this is a series of try/except blocks that try to read a series of different keys from an HDF5 file that may or may not have the individual keys, as in the example above. The attempt to read key2 will fail because the first attempt will have closed the file.

This issue was introduced in pull #25863 to address issue #25766 where read_hdf wasn't closing files that it hadn't opened itself.

The fix is pretty easy, just add `if not isinstance(path_or_buf, HDFStore):` before `store.close()` at the end of read_hdf().

#### Expected Output

Should not raise an Exception when reading key2.

#### Output of ``pd.show_versions()``

<details>

[paste the output of ``pd.show_versions()`` here below this line]

INSTALLED VERSIONS
------------------
commit           : None
python           : 3.7.4.final.0
python-bits      : 64
OS               : Linux
OS-release       : 3.10.0-957.27.2.el7.x86_64
machine          : x86_64
processor        : x86_64
byteorder        : little
LC_ALL           : en_US.utf8
LANG             : en_US.utf8
LOCALE           : en_US.UTF-8

pandas           : 0.25.1
numpy            : 1.17.2
pytz             : 2019.2
dateutil         : 2.8.0
pip              : 19.2.3
setuptools       : 41.0.1
Cython           : 0.29.13
pytest           : 5.1.2
hypothesis       : None
sphinx           : 2.2.0
blosc            : None
feather          : None
xlsxwriter       : None
lxml.etree       : None
html5lib         : None
pymysql          : None
psycopg2         : None
jinja2           : 2.10.1
IPython          : 7.8.0
pandas_datareader: None
bs4              : 4.8.0
bottleneck       : None
fastparquet      : None
gcsfs            : None
lxml.etree       : None
matplotlib       : 3.1.1
numexpr          : 2.7.0
odfpy            : None
openpyxl         : None
pandas_gbq       : None
pyarrow          : None
pytables         : None
s3fs             : None
scipy            : 1.3.1
sqlalchemy       : None
tables           : 3.5.2
xarray           : None
xlrd             : None
xlwt             : None
xlsxwriter       : None


</details>



Discussion:

Resolving patch:
diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py
--- 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
 


Test patch:
diff --git a/pandas/tests/io/pytables/test_store.py b/pandas/tests/io/pytables/test_store.py
--- a/pandas/tests/io/pytables/test_store.py
+++ b/pandas/tests/io/pytables/test_store.py
@@ -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