Skip to content

Commit

Permalink
BUG: disallow 'w' mode in pd.read_hdf (#13623) (#13858)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamsimha authored and jreback committed Aug 1, 2016
1 parent 78eadb7 commit 51e6adb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.19.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ API changes
- 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`)
- ``pd.Timedelta(None)`` is now accepted and will return ``NaT``, mirroring ``pd.Timestamp`` (:issue:`13687`)
- ``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`)
- ``pd.read_hdf`` will now raise a ``ValueError`` instead of ``KeyError``, if a mode other than ``r``, ``r+`` and ``a`` is supplied. (:issue:`13623`)


.. _whatsnew_0190.api.tolist:
Expand Down
4 changes: 4 additions & 0 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,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')))
# grab the scope
if 'where' in kwargs:
kwargs['where'] = _ensure_term(kwargs['where'], scope_level=1)
Expand Down
11 changes: 10 additions & 1 deletion pandas/io/tests/test_pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,16 +531,25 @@ def f():

# conv read
if mode in ['w']:
self.assertRaises(KeyError, read_hdf,
self.assertRaises(ValueError, read_hdf,
path, 'df', mode=mode)
else:
result = read_hdf(path, 'df', mode=mode)
assert_frame_equal(result, df)

def check_default_mode():

# read_hdf uses default mode
with ensure_clean_path(self.path) as path:
df.to_hdf(path, 'df', mode='w')
result = read_hdf(path, 'df')
assert_frame_equal(result, df)

check('r')
check('r+')
check('a')
check('w')
check_default_mode()

def test_reopen_handle(self):

Expand Down

0 comments on commit 51e6adb

Please sign in to comment.