Skip to content

Read hdf does not close store #25863

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 9 commits into from
Mar 31, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ I/O
- 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`)
- Bug in :func:`read_csv` not properly interpreting the UTF8 encoded filenames on Windows on Python 3.6+ (:issue:`15086`)
- Improved performance in :meth:`pandas.read_stata` and :class:`pandas.io.stata.StataReader` when converting columns that have missing values (:issue:`25772`)
- Bug in :func:`read_hdf` not properly close store when key is not found (:issue:`25766`)


Plotting
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def read_hdf(path_or_buf, key=None, mode='r', **kwargs):
'contains multiple datasets.')
key = candidate_only_group._v_pathname
return store.select(key, auto_close=auto_close, **kwargs)
except (ValueError, TypeError):
except (ValueError, TypeError, KeyError):
# if there is an error, close the store
try:
store.close()
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/io/test_pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,16 @@ def test_append_all_nans(self):
reloaded = read_hdf(path, 'df_with_missing')
tm.assert_frame_equal(df_with_missing, reloaded)

def test_read_missing_key_close_store(self):
# GH 25766
with ensure_clean_path(self.path) as path:
df = pd.DataFrame({'a': range(2), 'b': range(2)})
df.to_hdf(path, 'k1')

pytest.raises(KeyError, pd.read_hdf, path, 'k2')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not an accepted format any longer, use the context manager

with pytest.raises(....):
    ...

Copy link
Contributor Author

@rbenes rbenes Mar 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated 244cab4 . But with form pytest.raises(KeyError, ...) I followed the codestyle in this test file. But now I see some STY issues, that are trying to unify to context manager style 👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep this happens to be the last file - i think it’s fixed in another PR


df.to_hdf(path, 'k2')

def test_append_frame_column_oriented(self):

with ensure_clean_store(self.path) as store:
Expand Down