Skip to content

Commit c992be2

Browse files
committed
Fix pandas-dev#16583 by adding an explicit mode argument to read_hdf
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.
1 parent 4f68f93 commit c992be2

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

pandas/io/pytables.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -282,21 +282,23 @@ def to_hdf(path_or_buf, key, value, mode=None, complevel=None, complib=None,
282282
f(path_or_buf)
283283

284284

285-
def read_hdf(path_or_buf, key=None, **kwargs):
285+
def read_hdf(path_or_buf, key=None, mode='r', **kwargs):
286286
""" read from the store, close it if we opened it
287287
288288
Retrieve pandas object stored in file, optionally based on where
289289
criteria
290290
291291
Parameters
292292
----------
293-
path_or_buf : path (string), buffer, or path object (pathlib.Path or
294-
py._path.local.LocalPath) to read from
293+
path_or_buf : path (string), buffer, open pd.HDFStore, or path object
294+
(pathlib.Path or py._path.local.LocalPath) to read from
295295
296296
.. versionadded:: 0.19.0 support for pathlib, py.path.
297297
298298
key : group identifier in the store. Can be omitted if the HDF file
299299
contains a single pandas object.
300+
mode : Mode to use when opening the file (ignored if path_or_buf is
301+
a pd.HDFStore)
300302
where : list of Term (or convertable) objects, optional
301303
start : optional, integer (defaults to None), row number to start
302304
selection
@@ -313,10 +315,9 @@ def read_hdf(path_or_buf, key=None, **kwargs):
313315
314316
"""
315317

316-
if kwargs.get('mode', 'a') not in ['r', 'r+', 'a']:
318+
if mode not in ['r', 'r+', 'a']:
317319
raise ValueError('mode {0} is not allowed while performing a read. '
318-
'Allowed modes are r, r+ and a.'
319-
.format(kwargs.get('mode')))
320+
'Allowed modes are r, r+ and a.'.format(mode))
320321
# grab the scope
321322
if 'where' in kwargs:
322323
kwargs['where'] = _ensure_term(kwargs['where'], scope_level=1)
@@ -343,7 +344,7 @@ def read_hdf(path_or_buf, key=None, **kwargs):
343344
raise compat.FileNotFoundError(
344345
'File %s does not exist' % path_or_buf)
345346

346-
store = HDFStore(path_or_buf, **kwargs)
347+
store = HDFStore(path_or_buf, mode=mode, **kwargs)
347348
# can't auto open/close if we are using an iterator
348349
# so delegate to the iterator
349350
auto_close = True

0 commit comments

Comments
 (0)