Skip to content

Commit 7921b8d

Browse files
committed
Fixed pandas-dev#16583 by coercing mode to 'r'
Adjusted tests on warnings/errors to account for the change
1 parent bb8e1da commit 7921b8d

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

pandas/io/pytables.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -313,10 +313,10 @@ def read_hdf(path_or_buf, key=None, **kwargs):
313313
314314
"""
315315

316-
if kwargs.get('mode', 'a') not in ['r', 'r+', 'a']:
317-
raise ValueError('mode {0} is not allowed while performing a read. '
318-
'Allowed modes are r, r+ and a.'
319-
.format(kwargs.get('mode')))
316+
# Ignore any supplied mode, we only need to read data
317+
if kwargs.pop('mode', 'r') != 'r':
318+
warnings.warn('Ignoring requested mode to read_hdf, opening read-only')
319+
320320
# grab the scope
321321
if 'where' in kwargs:
322322
kwargs['where'] = _ensure_term(kwargs['where'], scope_level=1)
@@ -343,7 +343,7 @@ def read_hdf(path_or_buf, key=None, **kwargs):
343343
raise compat.FileNotFoundError(
344344
'File %s does not exist' % path_or_buf)
345345

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

pandas/tests/io/test_pytables.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -512,12 +512,12 @@ def f():
512512
df.to_hdf(path, 'df', mode=mode)
513513

514514
# conv read
515-
if mode in ['w']:
516-
pytest.raises(ValueError, read_hdf,
517-
path, 'df', mode=mode)
515+
if mode != 'r':
516+
with pytest.warns(UserWarning):
517+
result = read_hdf(path, 'df', mode=mode)
518518
else:
519519
result = read_hdf(path, 'df', mode=mode)
520-
assert_frame_equal(result, df)
520+
assert_frame_equal(result, df)
521521

522522
def check_default_mode():
523523

0 commit comments

Comments
 (0)