Skip to content

Commit

Permalink
Fix pandas-dev#16583 by adding an explicit mode argument to read_hdf
Browse files Browse the repository at this point in the history
kwargs which are meant for the opening of the HDFStore should be filtered out
before passing the remaining kwargs to the `select` function to load the data.
  • Loading branch information
frexvahi committed Jun 19, 2017
1 parent b95840f commit 5951379
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,21 +282,24 @@ def to_hdf(path_or_buf, key, value, mode=None, complevel=None, complib=None,
f(path_or_buf)


def read_hdf(path_or_buf, key=None, **kwargs):
def read_hdf(path_or_buf, key=None, mode='r', **kwargs):
""" read from the store, close it if we opened it
Retrieve pandas object stored in file, optionally based on where
criteria
Parameters
----------
path_or_buf : path (string), buffer, or path object (pathlib.Path or
py._path.local.LocalPath) to read from
path_or_buf : path (string), buffer or path object (pathlib.Path or
py._path.local.LocalPath) designating the file to open, or an
already opened pd.HDFStore object
.. versionadded:: 0.19.0 support for pathlib, py.path.
key : group identifier in the store. Can be omitted if the HDF file
contains a single pandas object.
mode : string, {'r', 'r+', 'a'}, default 'r'. Mode to use when opening
the file. Ignored if path_or_buf is a pd.HDFStore.
where : list of Term (or convertable) objects, optional
start : optional, integer (defaults to None), row number to start
selection
Expand All @@ -313,10 +316,9 @@ def read_hdf(path_or_buf, key=None, **kwargs):
"""

if kwargs.get('mode', 'a') not in ['r', 'r+', 'a']:
if mode not in ['r', 'r+', 'a']:
raise ValueError('mode {0} is not allowed while performing a read. '
'Allowed modes are r, r+ and a.'
.format(kwargs.get('mode')))
'Allowed modes are r, r+ and a.'.format(mode))
# grab the scope
if 'where' in kwargs:
kwargs['where'] = _ensure_term(kwargs['where'], scope_level=1)
Expand All @@ -343,7 +345,7 @@ def read_hdf(path_or_buf, key=None, **kwargs):
raise compat.FileNotFoundError(
'File %s does not exist' % path_or_buf)

store = HDFStore(path_or_buf, **kwargs)
store = HDFStore(path_or_buf, mode=mode, **kwargs)
# can't auto open/close if we are using an iterator
# so delegate to the iterator
auto_close = True
Expand Down

0 comments on commit 5951379

Please sign in to comment.