diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index 2c8582cf02ddf..ca5507058c0c1 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -461,7 +461,7 @@ def read_hdf( chunksize=chunksize, auto_close=auto_close, ) - except (ValueError, TypeError, KeyError): + except (ValueError, TypeError, KeyError, AttributeError): if not isinstance(path_or_buf, HDFStore): # if there is an error, close the store if we opened it. with suppress(AttributeError): diff --git a/pandas/tests/io/pytables/test_read.py b/pandas/tests/io/pytables/test_read.py index 1c9e63c66aadb..76a970be1a3b4 100644 --- a/pandas/tests/io/pytables/test_read.py +++ b/pandas/tests/io/pytables/test_read.py @@ -42,6 +42,22 @@ def test_read_missing_key_close_store(setup_path): df.to_hdf(path, "k2") +def test_read_missing_attribute_close_store(setup_path): + + with ensure_clean_path(setup_path) as path: + df = DataFrame() + with HDFStore(path) as s: + s.put("k", df) + s.get_storer("k").attributes.clear() + + with pytest.raises(AttributeError, match="object has no attribute"): + read_hdf(path, "k") + + # smoke test to test that file is properly closed after + # read with AttributeError before another write + df.to_hdf(path, "k") + + def test_read_missing_key_opened_store(setup_path): # GH 28699 with ensure_clean_path(setup_path) as path: