Skip to content

Commit b2845a1

Browse files
committed
Merge pull request #4513 from jreback/hdf_per
BUG: read_hdf was not respecting a passed mode (GH4504)
2 parents 69f5594 + 7f344fb commit b2845a1

File tree

3 files changed

+59
-8
lines changed

3 files changed

+59
-8
lines changed

doc/source/release.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,11 @@ pandas 0.13
109109

110110
**Bug Fixes**
111111

112-
- ``HDFStore`` raising an invalid ``TypeError`` rather than ``ValueError`` when appending
113-
with a different block ordering (:issue:`4096`)
112+
- ``HDFStore``
113+
114+
- raising an invalid ``TypeError`` rather than ``ValueError`` when appending
115+
with a different block ordering (:issue:`4096`)
116+
- ``read_hdf`` was not respecting as passed ``mode`` (:issue:`4504`)
114117
- The ``by`` argument now works correctly with the ``layout`` argument
115118
(:issue:`4102`, :issue:`4014`) in ``*.hist`` plotting methods
116119
- Fixed bug in ``PeriodIndex.map`` where using ``str`` would return the str

pandas/io/pytables.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,7 @@ def h5_open(path, mode):
167167

168168

169169
@contextmanager
170-
def get_store(path, mode='a', complevel=None, complib=None,
171-
fletcher32=False):
170+
def get_store(path, **kwargs):
172171
"""
173172
Creates an HDFStore instance. This function can be used in a with statement
174173
@@ -184,8 +183,7 @@ def get_store(path, mode='a', complevel=None, complib=None,
184183
"""
185184
store = None
186185
try:
187-
store = HDFStore(path, mode=mode, complevel=complevel,
188-
complib=complib, fletcher32=False)
186+
store = HDFStore(path, **kwargs)
189187
yield store
190188
finally:
191189
if store is not None:
@@ -215,7 +213,7 @@ def read_hdf(path_or_buf, key, **kwargs):
215213

216214
# can't auto open/close if we are using an iterator
217215
# so delegate to the iterator
218-
store = HDFStore(path_or_buf)
216+
store = HDFStore(path_or_buf,**kwargs)
219217
try:
220218
return f(store, True)
221219
except:
@@ -274,7 +272,7 @@ class HDFStore(StringMixin):
274272
"""
275273

276274
def __init__(self, path, mode=None, complevel=None, complib=None,
277-
fletcher32=False):
275+
fletcher32=False, **kwargs):
278276
try:
279277
import tables as _
280278
except ImportError: # pragma: no cover

pandas/io/tests/test_pytables.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,56 @@ def test_versioning(self):
234234
store.get_node('df2')._v_attrs.pandas_version = None
235235
self.assertRaises(Exception, store.select, 'df2')
236236

237+
def test_mode(self):
238+
239+
df = tm.makeTimeDataFrame()
240+
241+
def check(mode):
242+
243+
with tm.ensure_clean(self.path) as path:
244+
245+
# constructor
246+
if mode in ['r','r+']:
247+
self.assertRaises(IOError, HDFStore, path, mode=mode)
248+
249+
else:
250+
store = HDFStore(path,mode=mode)
251+
self.assert_(store._handle.mode == mode)
252+
store.close()
253+
254+
with tm.ensure_clean(self.path) as path:
255+
256+
# context
257+
if mode in ['r','r+']:
258+
def f():
259+
with get_store(path,mode=mode) as store:
260+
pass
261+
self.assertRaises(IOError, f)
262+
else:
263+
with get_store(path,mode=mode) as store:
264+
self.assert_(store._handle.mode == mode)
265+
266+
with tm.ensure_clean(self.path) as path:
267+
268+
# conv write
269+
if mode in ['r','r+']:
270+
self.assertRaises(IOError, df.to_hdf, path, 'df', mode=mode)
271+
df.to_hdf(path,'df',mode='w')
272+
else:
273+
df.to_hdf(path,'df',mode=mode)
274+
275+
# conv read
276+
if mode in ['w']:
277+
self.assertRaises(KeyError, read_hdf, path, 'df', mode=mode)
278+
else:
279+
result = read_hdf(path,'df',mode=mode)
280+
assert_frame_equal(result,df)
281+
282+
check('r')
283+
check('r+')
284+
check('a')
285+
check('w')
286+
237287
def test_reopen_handle(self):
238288

239289
with tm.ensure_clean(self.path) as path:

0 commit comments

Comments
 (0)