Skip to content

Commit e0e1384

Browse files
authored
replace KeyError with LookupError in the types of possible read_hdf errors (#52783)
* add IndexError to the types of possible read_hdf errors fixes #52781 * test that IndexError closes hdf files * coalesce KeyError and IndexError into LookupError * add news entry * avoid pd. prefix * sort changelog
1 parent e96054a commit e0e1384

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ I/O
348348
^^^
349349
- :meth:`DataFrame.to_orc` now raising ``ValueError`` when non-default :class:`Index` is given (:issue:`51828`)
350350
- :meth:`DataFrame.to_sql` now raising ``ValueError`` when the name param is left empty while using SQLAlchemy to connect (:issue:`52675`)
351+
- Bug in :func:`read_hdf` not properly closing store after a ``IndexError`` is raised (:issue:`52781`)
351352
- Bug in :func:`read_html`, style elements were read into DataFrames (:issue:`52197`)
352353
- Bug in :func:`read_html`, tail texts were removed together with elements containing ``display:none`` style (:issue:`51629`)
353354
-

pandas/io/pytables.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ def read_hdf(
455455
chunksize=chunksize,
456456
auto_close=auto_close,
457457
)
458-
except (ValueError, TypeError, KeyError):
458+
except (ValueError, TypeError, LookupError):
459459
if not isinstance(path_or_buf, HDFStore):
460460
# if there is an error, close the store if we opened it.
461461
with suppress(AttributeError):

pandas/tests/io/pytables/test_read.py

+14
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,20 @@ def test_read_missing_key_close_store(tmp_path, setup_path):
4242
df.to_hdf(path, "k2")
4343

4444

45+
def test_read_index_error_close_store(tmp_path, setup_path):
46+
# GH 25766
47+
path = tmp_path / setup_path
48+
df = DataFrame({"A": [], "B": []}, index=[])
49+
df.to_hdf(path, "k1")
50+
51+
with pytest.raises(IndexError, match=r"list index out of range"):
52+
read_hdf(path, "k1", stop=0)
53+
54+
# smoke test to test that file is properly closed after
55+
# read with IndexError before another write
56+
df.to_hdf(path, "k1")
57+
58+
4559
def test_read_missing_key_opened_store(tmp_path, setup_path):
4660
# GH 28699
4761
path = tmp_path / setup_path

0 commit comments

Comments
 (0)