Skip to content

BUG: read_hdf was not respecting a passed mode (GH4504) #4513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 12, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 4 additions & 6 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
50 changes: 50 additions & 0 deletions pandas/io/tests/test_pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down