Skip to content

pandas.read_hdf leaves file open when requested data key does not exist #26943

Closed
@RomanKoziolek

Description

@RomanKoziolek

Code Sample, a copy-pastable example if possible

import pandas

data = pandas.read_hdf(<h5_file_name>, mode='r', key=key)

Problem description

When trying to read HDF5 file and the requested key object does not exist in the file, pandas pytables raises KeyError('No object named {key} in the file'.) and does not close the opened file. I would expect that the HDF5 file is closed after trying to read the key object, whether it was successful or not.
Trying to write to the file which is opened in read mode fails with exception.
The problematic code is in pandas.io.pytables.py file, method select, line 712.

Workaround

Check if the key exists before trying to read it from file.

import h5py
key_exists = True
with h5py.File(<h5_file_name>, mode='r') as file:
    group = file.get(key)
    if not group:
       key_exists = False

if key_exists:
    ...

Should you need more details, please let me know.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions