Skip to content

read_hdf does not close store when raise KeyError #25766

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

Closed
xmduhan opened this issue Mar 18, 2019 · 1 comment · Fixed by #25863
Closed

read_hdf does not close store when raise KeyError #25766

xmduhan opened this issue Mar 18, 2019 · 1 comment · Fixed by #25863
Labels
Bug IO HDF5 read_hdf, HDFStore
Milestone

Comments

@xmduhan
Copy link

xmduhan commented Mar 18, 2019

Code Sample, a copy-pastable example if possible

import pandas as pd
df = pd.DataFrame({'a': range(10), 'b': range(10)})

df.to_hdf('/tmp/1.hdf', 'k1')
try:
    pd.read_hdf('/tmp/1.hdf', 'k2')
except Exception as e:
    print(type(e))
    print(e)
    
df.to_hdf('/tmp/1.hdf', 'k2')

Problem description

<type 'exceptions.KeyError'>
'No object named k2 in the file'

The file '/tmp/1.hdf' is already opened, but in read-only mode. Please close it before reopening in append mode.

Expected Output

should not raise Exception when: df.to_hdf('/tmp/1.hdf', 'k2')

pandas/io/pytables.py line 408

 except (ValueError, TypeError):  # should also catch KeyError 

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 2.7.12.final.0
python-bits: 64
OS: Linux
OS-release: 4.4.0-141-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: zh_CN.UTF-8
LOCALE: None.None

pandas: 0.24.2
pytest: 3.2.4
pip: 19.0.1
setuptools: 39.1.0
Cython: 0.27.3
numpy: 1.14.5
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 5.5.0
sphinx: 1.6.5
patsy: 0.4.1
dateutil: 2.6.1
pytz: 2017.3
blosc: None
bottleneck: None
tables: 3.4.2
numexpr: 2.6.4
feather: None
matplotlib: 2.1.0
openpyxl: 2.4.9
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.0.2
lxml.etree: 4.1.1
bs4: 4.6.0
html5lib: 0.9999999
sqlalchemy: 1.1.15
pymysql: None
psycopg2: 2.7.3.2 (dt dec pq3 ext lo64)
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None

@WillAyd
Copy link
Member

WillAyd commented Mar 19, 2019

Thanks for the report! PRs are always welcome

@WillAyd WillAyd added IO HDF5 read_hdf, HDFStore Bug labels Mar 19, 2019
@xmduhan xmduhan changed the title read_hdf does not close stone when raise KeyError read_hdf does not close store when raise KeyError Mar 19, 2019
@jreback jreback added this to the 0.25.0 milestone Mar 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug IO HDF5 read_hdf, HDFStore
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants