Skip to content
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

Hdf with #1005

Closed
wants to merge 3 commits into from
Closed
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
47 changes: 47 additions & 0 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from pandas.core.common import adjoin
import pandas.core.common as com
import pandas._tseries as lib
from contextlib import contextmanager

# reading and writing the full object in one go
_TYPE_MAP = {
Expand Down Expand Up @@ -55,6 +56,52 @@ def _tables():
_table_mod = tables
return _table_mod

@contextmanager
def get_store(path, mode='a', complevel=None, complib=None,
fletcher32=False):
"""
Creates an HDFStore instance. This function can be used in a with statement

Parameters
----------
path : string
File path to HDF5 file
mode : {'a', 'w', 'r', 'r+'}, default 'a'

``'r'``
Read-only; no data can be modified.
``'w'``
Write; a new file is created (an existing file with the same
name would be deleted).
``'a'``
Append; an existing file is opened for reading and writing,
and if the file does not exist it is created.
``'r+'``
It is similar to ``'a'``, but the file must already exist.
complevel : int, 1-9, default 0
If a complib is specified compression will be applied
where possible
complib : {'zlib', 'bzip2', 'lzo', 'blosc', None}, default None
If complevel is > 0 apply compression to objects written
in the store wherever possible
fletcher32 : bool, default False
If applying compression use the fletcher32 checksum

Examples
--------
>>> with get_store('test.h5') as store:
>>> store['foo'] = bar # write to HDF5
>>> bar = store['foo'] # retrieve
"""
store = None
try:
store = HDFStore(path, mode=mode, complevel=complevel,
complib=complib, fletcher32=False)
yield store
finally:
if store is not None:
store.close()

class HDFStore(object):
"""
dict-like IO interface for storing pandas objects in PyTables
Expand Down
20 changes: 19 additions & 1 deletion pandas/io/tests/test_pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import numpy as np

from pandas import Series, DataFrame, Panel, DateRange, MultiIndex
from pandas.io.pytables import HDFStore
from pandas.io.pytables import HDFStore, get_store
import pandas.util.testing as tm

try:
Expand All @@ -31,6 +31,22 @@ def tearDown(self):
self.store.close()
os.remove(self.path)

def test_factory_fun(self):
try:
with get_store(self.scratchpath) as tbl:
raise ValueError('blah')
except ValueError:
pass

with get_store(self.scratchpath) as tbl:
tbl['a'] = tm.makeDataFrame()

with get_store(self.scratchpath) as tbl:
self.assertEquals(len(tbl), 1)
self.assertEquals(type(tbl['a']), DataFrame)

os.remove(self.scratchpath)

def test_len_keys(self):
self.store['a'] = tm.makeTimeSeries()
self.store['b'] = tm.makeStringSeries()
Expand Down Expand Up @@ -473,6 +489,8 @@ def test_store_datetime_fractional_secs(self):
self.store['a'] = series
self.assertEquals(self.store['a'].index[0], dt)



def curpath():
pth, _ = os.path.split(os.path.abspath(__file__))
return pth
Expand Down