Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e01bcc3

Browse files
rbenesWillAyd
authored andcommittedMar 31, 2019
Read hdf does not close store (#25863)
1 parent de3a85c commit e01bcc3

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed
 

‎doc/source/whatsnew/v0.25.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ I/O
353353
- Bug in :meth:`DataFrame.to_string` and :meth:`DataFrame.to_latex` that would lead to incorrect output when the ``header`` keyword is used (:issue:`16718`)
354354
- Bug in :func:`read_csv` not properly interpreting the UTF8 encoded filenames on Windows on Python 3.6+ (:issue:`15086`)
355355
- Improved performance in :meth:`pandas.read_stata` and :class:`pandas.io.stata.StataReader` when converting columns that have missing values (:issue:`25772`)
356+
- Bug in :func:`read_hdf` not properly closing store after a ``KeyError`` is raised (:issue:`25766`)
356357
- Bug in ``read_csv`` which would not raise ``ValueError`` if a column index in ``usecols`` was out of bounds (:issue:`25623`)
357358

358359
Plotting

‎pandas/io/pytables.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ def read_hdf(path_or_buf, key=None, mode='r', **kwargs):
375375
'contains multiple datasets.')
376376
key = candidate_only_group._v_pathname
377377
return store.select(key, auto_close=auto_close, **kwargs)
378-
except (ValueError, TypeError):
378+
except (ValueError, TypeError, KeyError):
379379
# if there is an error, close the store
380380
try:
381381
store.close()

‎pandas/tests/io/test_pytables.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,19 @@ def test_append_all_nans(self):
12331233
reloaded = read_hdf(path, 'df_with_missing')
12341234
tm.assert_frame_equal(df_with_missing, reloaded)
12351235

1236+
def test_read_missing_key_close_store(self):
1237+
# GH 25766
1238+
with ensure_clean_path(self.path) as path:
1239+
df = pd.DataFrame({'a': range(2), 'b': range(2)})
1240+
df.to_hdf(path, 'k1')
1241+
1242+
with pytest.raises(KeyError):
1243+
pd.read_hdf(path, 'k2')
1244+
1245+
# smoke test to test that file is properly closed after
1246+
# read with KeyError before another write
1247+
df.to_hdf(path, 'k2')
1248+
12361249
def test_append_frame_column_oriented(self):
12371250

12381251
with ensure_clean_store(self.path) as store:

0 commit comments

Comments
 (0)
Please sign in to comment.