Skip to content

Commit 2e63fe2

Browse files
committed
Merge branch '8791' of https://github.com/vikram/pandas into vikram-8791
2 parents 68b4618 + 924c62e commit 2e63fe2

File tree

4 files changed

+38
-33
lines changed

4 files changed

+38
-33
lines changed

doc/source/io.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -2348,7 +2348,7 @@ Closing a Store, Context Manager
23482348
23492349
# Working with, and automatically closing the store with the context
23502350
# manager
2351-
with get_store('store.h5') as store:
2351+
with HDFStore('store.h5') as store:
23522352
store.keys()
23532353
23542354
.. ipython:: python

doc/source/whatsnew/v0.15.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ Enhancements
7777
- Added ``Timedelta.to_timedelta64`` method to the public API (:issue:`8884`).
7878
- Added ``gbq.generate_bq_schema`` function to the gbq module (:issue:`8325`).
7979
- ``Series`` now works with map objects the same way as generators (:issue:`8909`).
80+
- Added context manager to ``HDFStore`` for automatic closing (:issue:`8791`).
8081

8182
.. _whatsnew_0152.performance:
8283

pandas/io/pytables.py

+13-28
Original file line numberDiff line numberDiff line change
@@ -251,33 +251,6 @@ def _tables():
251251

252252
return _table_mod
253253

254-
@contextmanager
255-
def get_store(path, **kwargs):
256-
"""
257-
Creates an HDFStore instance. This function can be used in a with statement
258-
259-
Parameters
260-
----------
261-
same as HDFStore
262-
263-
Examples
264-
--------
265-
>>> from pandas import DataFrame
266-
>>> from numpy.random import randn
267-
>>> bar = DataFrame(randn(10, 4))
268-
>>> with get_store('test.h5') as store:
269-
... store['foo'] = bar # write to HDF5
270-
... bar = store['foo'] # retrieve
271-
"""
272-
store = None
273-
try:
274-
store = HDFStore(path, **kwargs)
275-
yield store
276-
finally:
277-
if store is not None:
278-
store.close()
279-
280-
281254
# interface to/from ###
282255

283256
def to_hdf(path_or_buf, key, value, mode=None, complevel=None, complib=None,
@@ -289,7 +262,7 @@ def to_hdf(path_or_buf, key, value, mode=None, complevel=None, complib=None,
289262
f = lambda store: store.put(key, value, **kwargs)
290263

291264
if isinstance(path_or_buf, string_types):
292-
with get_store(path_or_buf, mode=mode, complevel=complevel,
265+
with HDFStore(path_or_buf, mode=mode, complevel=complevel,
293266
complib=complib) as store:
294267
f(store)
295268
else:
@@ -493,6 +466,12 @@ def __unicode__(self):
493466

494467
return output
495468

469+
def __enter__(self):
470+
return self
471+
472+
def __exit__(self, exc_type, exc_value, traceback):
473+
self.close()
474+
496475
def keys(self):
497476
"""
498477
Return a (potentially unordered) list of the keys corresponding to the
@@ -1288,6 +1267,12 @@ def _read_group(self, group, **kwargs):
12881267
return s.read(**kwargs)
12891268

12901269

1270+
def get_store(path, **kwargs):
1271+
""" Backwards compatible alias for ``HDFStore``
1272+
"""
1273+
return HDFStore(path, **kwargs)
1274+
1275+
12911276
class TableIterator(object):
12921277

12931278
""" define the iteration interface on a table

pandas/io/tests/test_pytables.py

+23-4
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,25 @@ def test_factory_fun(self):
172172
finally:
173173
safe_remove(self.path)
174174

175+
def test_context(self):
176+
try:
177+
with HDFStore(self.path) as tbl:
178+
raise ValueError('blah')
179+
except ValueError:
180+
pass
181+
finally:
182+
safe_remove(self.path)
183+
184+
try:
185+
with HDFStore(self.path) as tbl:
186+
tbl['a'] = tm.makeDataFrame()
187+
188+
with HDFStore(self.path) as tbl:
189+
self.assertEqual(len(tbl), 1)
190+
self.assertEqual(type(tbl['a']), DataFrame)
191+
finally:
192+
safe_remove(self.path)
193+
175194
def test_conv_read_write(self):
176195

177196
try:
@@ -334,10 +353,10 @@ def test_api_default_format(self):
334353

335354
pandas.set_option('io.hdf.default_format','table')
336355
df.to_hdf(path,'df3')
337-
with get_store(path) as store:
356+
with HDFStore(path) as store:
338357
self.assertTrue(store.get_storer('df3').is_table)
339358
df.to_hdf(path,'df4',append=True)
340-
with get_store(path) as store:
359+
with HDFStore(path) as store:
341360
self.assertTrue(store.get_storer('df4').is_table)
342361

343362
pandas.set_option('io.hdf.default_format',None)
@@ -463,11 +482,11 @@ def check(mode):
463482
# context
464483
if mode in ['r','r+']:
465484
def f():
466-
with get_store(path,mode=mode) as store:
485+
with HDFStore(path,mode=mode) as store:
467486
pass
468487
self.assertRaises(IOError, f)
469488
else:
470-
with get_store(path,mode=mode) as store:
489+
with HDFStore(path,mode=mode) as store:
471490
self.assertEqual(store._handle.mode, mode)
472491

473492
with ensure_clean_path(self.path) as path:

0 commit comments

Comments
 (0)