@@ -282,21 +282,24 @@ def to_hdf(path_or_buf, key, value, mode=None, complevel=None, complib=None,
282
282
f (path_or_buf )
283
283
284
284
285
- def read_hdf (path_or_buf , key = None , ** kwargs ):
285
+ def read_hdf (path_or_buf , key = None , mode = 'r' , ** kwargs ):
286
286
""" read from the store, close it if we opened it
287
287
288
288
Retrieve pandas object stored in file, optionally based on where
289
289
criteria
290
290
291
291
Parameters
292
292
----------
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 or path object (pathlib.Path or
294
+ py._path.local.LocalPath) designating the file to open, or an
295
+ already opened pd.HDFStore object
295
296
296
297
.. versionadded:: 0.19.0 support for pathlib, py.path.
297
298
298
299
key : group identifier in the store. Can be omitted if the HDF file
299
300
contains a single pandas object.
301
+ mode : string, {'r', 'r+', 'a'}, default 'r'. Mode to use when opening
302
+ the file. Ignored if path_or_buf is a pd.HDFStore.
300
303
where : list of Term (or convertable) objects, optional
301
304
start : optional, integer (defaults to None), row number to start
302
305
selection
@@ -313,10 +316,9 @@ def read_hdf(path_or_buf, key=None, **kwargs):
313
316
314
317
"""
315
318
316
- if kwargs . get ( ' mode' , 'a' ) not in ['r' , 'r+' , 'a' ]:
319
+ if mode not in ['r' , 'r+' , 'a' ]:
317
320
raise ValueError ('mode {0} is not allowed while performing a read. '
318
- 'Allowed modes are r, r+ and a.'
319
- .format (kwargs .get ('mode' )))
321
+ 'Allowed modes are r, r+ and a.' .format (mode ))
320
322
# grab the scope
321
323
if 'where' in kwargs :
322
324
kwargs ['where' ] = _ensure_term (kwargs ['where' ], scope_level = 1 )
@@ -343,7 +345,7 @@ def read_hdf(path_or_buf, key=None, **kwargs):
343
345
raise compat .FileNotFoundError (
344
346
'File %s does not exist' % path_or_buf )
345
347
346
- store = HDFStore (path_or_buf , ** kwargs )
348
+ store = HDFStore (path_or_buf , mode = mode , ** kwargs )
347
349
# can't auto open/close if we are using an iterator
348
350
# so delegate to the iterator
349
351
auto_close = True
0 commit comments