From 7921b8d467ecd1c7089c2b63cf2d365f05aff0d6 Mon Sep 17 00:00:00 2001 From: Chris Kerr Date: Tue, 6 Jun 2017 10:26:12 +0200 Subject: [PATCH] Fixed #16583 by coercing mode to 'r' Adjusted tests on warnings/errors to account for the change --- pandas/io/pytables.py | 10 +++++----- pandas/tests/io/test_pytables.py | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index 4a1b12414bcc56..7b2dd3c1a15063 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -313,10 +313,10 @@ def read_hdf(path_or_buf, key=None, **kwargs): """ - if kwargs.get('mode', 'a') 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'))) + # Ignore any supplied mode, we only need to read data + if kwargs.pop('mode', 'r') != 'r': + warnings.warn('Ignoring requested mode to read_hdf, opening read-only') + # grab the scope if 'where' in kwargs: kwargs['where'] = _ensure_term(kwargs['where'], scope_level=1) @@ -343,7 +343,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='r', **kwargs) # can't auto open/close if we are using an iterator # so delegate to the iterator auto_close = True diff --git a/pandas/tests/io/test_pytables.py b/pandas/tests/io/test_pytables.py index 15fbdf59bfe35f..5daee90e549e93 100644 --- a/pandas/tests/io/test_pytables.py +++ b/pandas/tests/io/test_pytables.py @@ -512,12 +512,12 @@ def f(): df.to_hdf(path, 'df', mode=mode) # conv read - if mode in ['w']: - pytest.raises(ValueError, read_hdf, - path, 'df', mode=mode) + if mode != 'r': + with pytest.warns(UserWarning): + result = read_hdf(path, 'df', mode=mode) else: result = read_hdf(path, 'df', mode=mode) - assert_frame_equal(result, df) + assert_frame_equal(result, df) def check_default_mode():