Skip to content

Commit 2cb0e47

Browse files
committed
Fixed #16583 by coercing mode to 'r'
Adjusted tests on warnings/errors to account for the change
1 parent bb8e1da commit 2cb0e47

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

pandas/io/pytables.py

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

316-
if kwargs.get('mode', 'a') not in ['r', 'r+', 'a']:
316+
# Ignore any supplied mode, we only need to read data
317+
if kwargs.get('mode', 'r') not in ['r', 'r+', 'a']:
317318
raise ValueError('mode {0} is not allowed while performing a read. '
318319
'Allowed modes are r, r+ and a.'
319320
.format(kwargs.get('mode')))
321+
if kwargs.pop('mode', 'r') != 'r':
322+
warnings.warn('Ignoring requested mode to read_hdf, opening read-only')
323+
320324
# grab the scope
321325
if 'where' in kwargs:
322326
kwargs['where'] = _ensure_term(kwargs['where'], scope_level=1)
@@ -343,7 +347,7 @@ def read_hdf(path_or_buf, key=None, **kwargs):
343347
raise compat.FileNotFoundError(
344348
'File %s does not exist' % path_or_buf)
345349

346-
store = HDFStore(path_or_buf, **kwargs)
350+
store = HDFStore(path_or_buf, mode='r', **kwargs)
347351
# can't auto open/close if we are using an iterator
348352
# so delegate to the iterator
349353
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)