Skip to content

Commit b03d90b

Browse files
Jayasimha Tiamsimha
Jayasimha T
authored andcommitted
BUG: disallow 'w' mode in pd.read_hdf (#13623)
1 parent 59f2557 commit b03d90b

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

doc/source/whatsnew/v0.19.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ API changes
379379
- The ``pd.read_json`` and ``DataFrame.to_json`` has gained support for reading and writing json lines with ``lines`` option see :ref:`Line delimited json <io.jsonl>` (:issue:`9180`)
380380
- ``pd.Timedelta(None)`` is now accepted and will return ``NaT``, mirroring ``pd.Timestamp`` (:issue:`13687`)
381381
- ``Timestamp``, ``Period``, ``DatetimeIndex``, ``PeriodIndex`` and ``.dt`` accessor have gained a ``.is_leap_year`` property to check whether the date belongs to a leap year. (:issue:`13727`)
382+
- ``pd.read_hdf`` will now raise a ``ValueError`` instead of ``KeyError``, if a mode other than ``r``, ``r+`` and ``a`` is supplied. (:issue:`13623`)
382383

383384

384385
.. _whatsnew_0190.api.tolist:

pandas/io/pytables.py

+4
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,10 @@ def read_hdf(path_or_buf, key=None, **kwargs):
303303
304304
"""
305305

306+
if kwargs.get('mode', 'a') not in ['r', 'r+', 'a']:
307+
raise ValueError('mode {0} is not allowed while performing a read. '
308+
'Allowed modes are r, r+ and a.'
309+
.format(kwargs.get('mode')))
306310
# grab the scope
307311
if 'where' in kwargs:
308312
kwargs['where'] = _ensure_term(kwargs['where'], scope_level=1)

pandas/io/tests/test_pytables.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -531,16 +531,25 @@ def f():
531531

532532
# conv read
533533
if mode in ['w']:
534-
self.assertRaises(KeyError, read_hdf,
534+
self.assertRaises(ValueError, read_hdf,
535535
path, 'df', mode=mode)
536536
else:
537537
result = read_hdf(path, 'df', mode=mode)
538538
assert_frame_equal(result, df)
539539

540+
def check_default_mode():
541+
542+
# read_hdf uses default mode
543+
with ensure_clean_path(self.path) as path:
544+
df.to_hdf(path, 'df', mode='w')
545+
result = read_hdf(path, 'df')
546+
assert_frame_equal(result, df)
547+
540548
check('r')
541549
check('r+')
542550
check('a')
543551
check('w')
552+
check_default_mode()
544553

545554
def test_reopen_handle(self):
546555

0 commit comments

Comments
 (0)