@@ -282,21 +282,23 @@ 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, open pd.HDFStore, or path object
294
+ (pathlib.Path or py._path.local.LocalPath) to read from
295
295
296
296
.. versionadded:: 0.19.0 support for pathlib, py.path.
297
297
298
298
key : group identifier in the store. Can be omitted if the HDF file
299
299
contains a single pandas object.
300
+ mode : Mode to use when opening the file (ignored if path_or_buf is
301
+ a pd.HDFStore)
300
302
where : list of Term (or convertable) objects, optional
301
303
start : optional, integer (defaults to None), row number to start
302
304
selection
@@ -313,10 +315,9 @@ def read_hdf(path_or_buf, key=None, **kwargs):
313
315
314
316
"""
315
317
316
- if kwargs . get ( ' mode' , 'a' ) not in ['r' , 'r+' , 'a' ]:
318
+ if mode not in ['r' , 'r+' , 'a' ]:
317
319
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 ))
320
321
# grab the scope
321
322
if 'where' in kwargs :
322
323
kwargs ['where' ] = _ensure_term (kwargs ['where' ], scope_level = 1 )
@@ -343,7 +344,7 @@ def read_hdf(path_or_buf, key=None, **kwargs):
343
344
raise compat .FileNotFoundError (
344
345
'File %s does not exist' % path_or_buf )
345
346
346
- store = HDFStore (path_or_buf , ** kwargs )
347
+ store = HDFStore (path_or_buf , mode = mode , ** kwargs )
347
348
# can't auto open/close if we are using an iterator
348
349
# so delegate to the iterator
349
350
auto_close = True
0 commit comments