From 7f344fb180fe1158a3c47778b4fe16d2cdcedba7 Mon Sep 17 00:00:00 2001 From: jreback Date: Thu, 8 Aug 2013 08:01:51 -0400 Subject: [PATCH] BUG: read_hdf was not respecting as passed mode (GH4504) --- doc/source/release.rst | 7 +++-- pandas/io/pytables.py | 10 +++---- pandas/io/tests/test_pytables.py | 50 ++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 8 deletions(-) diff --git a/doc/source/release.rst b/doc/source/release.rst index 95ce03a858570..01d27cc113e51 100644 --- a/doc/source/release.rst +++ b/doc/source/release.rst @@ -87,8 +87,11 @@ pandas 0.13 **Bug Fixes** - - ``HDFStore`` raising an invalid ``TypeError`` rather than ``ValueError`` when appending - with a different block ordering (:issue:`4096`) + - ``HDFStore`` + + - raising an invalid ``TypeError`` rather than ``ValueError`` when appending + with a different block ordering (:issue:`4096`) + - ``read_hdf`` was not respecting as passed ``mode`` (:issue:`4504`) - The ``by`` argument now works correctly with the ``layout`` argument (:issue:`4102`, :issue:`4014`) in ``*.hist`` plotting methods - Fixed bug in ``PeriodIndex.map`` where using ``str`` would return the str diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index 4295139965f81..9990da148f8a3 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -167,8 +167,7 @@ def h5_open(path, mode): @contextmanager -def get_store(path, mode='a', complevel=None, complib=None, - fletcher32=False): +def get_store(path, **kwargs): """ Creates an HDFStore instance. This function can be used in a with statement @@ -184,8 +183,7 @@ def get_store(path, mode='a', complevel=None, complib=None, """ store = None try: - store = HDFStore(path, mode=mode, complevel=complevel, - complib=complib, fletcher32=False) + store = HDFStore(path, **kwargs) yield store finally: if store is not None: @@ -215,7 +213,7 @@ def read_hdf(path_or_buf, key, **kwargs): # can't auto open/close if we are using an iterator # so delegate to the iterator - store = HDFStore(path_or_buf) + store = HDFStore(path_or_buf,**kwargs) try: return f(store, True) except: @@ -274,7 +272,7 @@ class HDFStore(StringMixin): """ def __init__(self, path, mode=None, complevel=None, complib=None, - fletcher32=False): + fletcher32=False, **kwargs): try: import tables as _ except ImportError: # pragma: no cover diff --git a/pandas/io/tests/test_pytables.py b/pandas/io/tests/test_pytables.py index 9575d99229dc4..9c09fe965a20f 100644 --- a/pandas/io/tests/test_pytables.py +++ b/pandas/io/tests/test_pytables.py @@ -234,6 +234,56 @@ def test_versioning(self): store.get_node('df2')._v_attrs.pandas_version = None self.assertRaises(Exception, store.select, 'df2') + def test_mode(self): + + df = tm.makeTimeDataFrame() + + def check(mode): + + with tm.ensure_clean(self.path) as path: + + # constructor + if mode in ['r','r+']: + self.assertRaises(IOError, HDFStore, path, mode=mode) + + else: + store = HDFStore(path,mode=mode) + self.assert_(store._handle.mode == mode) + store.close() + + with tm.ensure_clean(self.path) as path: + + # context + if mode in ['r','r+']: + def f(): + with get_store(path,mode=mode) as store: + pass + self.assertRaises(IOError, f) + else: + with get_store(path,mode=mode) as store: + self.assert_(store._handle.mode == mode) + + with tm.ensure_clean(self.path) as path: + + # conv write + if mode in ['r','r+']: + self.assertRaises(IOError, df.to_hdf, path, 'df', mode=mode) + df.to_hdf(path,'df',mode='w') + else: + df.to_hdf(path,'df',mode=mode) + + # conv read + if mode in ['w']: + self.assertRaises(KeyError, read_hdf, path, 'df', mode=mode) + else: + result = read_hdf(path,'df',mode=mode) + assert_frame_equal(result,df) + + check('r') + check('r+') + check('a') + check('w') + def test_reopen_handle(self): with tm.ensure_clean(self.path) as path: