@@ -282,21 +282,24 @@ 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 or path object (pathlib.Path or
294+ py._path.local.LocalPath) designating the file to open, or an
295+ already opened pd.HDFStore object
295296
296297 .. versionadded:: 0.19.0 support for pathlib, py.path.
297298
298299 key : group identifier in the store. Can be omitted if the HDF file
299300 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.
300303 where : list of Term (or convertable) objects, optional
301304 start : optional, integer (defaults to None), row number to start
302305 selection
@@ -313,10 +316,9 @@ def read_hdf(path_or_buf, key=None, **kwargs):
313316
314317 """
315318
316- if kwargs . get ( ' mode' , 'a' ) not in ['r' , 'r+' , 'a' ]:
319+ if mode not in ['r' , 'r+' , 'a' ]:
317320 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 ))
320322 # grab the scope
321323 if 'where' in kwargs :
322324 kwargs ['where' ] = _ensure_term (kwargs ['where' ], scope_level = 1 )
@@ -343,7 +345,7 @@ def read_hdf(path_or_buf, key=None, **kwargs):
343345 raise compat .FileNotFoundError (
344346 'File %s does not exist' % path_or_buf )
345347
346- store = HDFStore (path_or_buf , ** kwargs )
348+ store = HDFStore (path_or_buf , mode = mode , ** kwargs )
347349 # can't auto open/close if we are using an iterator
348350 # so delegate to the iterator
349351 auto_close = True
0 commit comments