Repository: pandas-dev/pandas

Issue:
ENH: read_hdf / to_hdf
add in HDFStore, import in core.api

provide same API as append/select

use get_store to open/close files



Discussion:

Resolving patch:
diff --git a/pandas/__init__.py b/pandas/__init__.py
--- a/pandas/__init__.py
+++ b/pandas/__init__.py
@@ -32,7 +32,7 @@
 from pandas.io.parsers import (read_csv, read_table, read_clipboard,
                                read_fwf, to_clipboard, ExcelFile,
                                ExcelWriter)
-from pandas.io.pytables import HDFStore, Term, get_store
+from pandas.io.pytables import HDFStore, Term, get_store, read_hdf
 from pandas.util.testing import debug
 
 from pandas.tools.describe import value_range
diff --git a/pandas/core/generic.py b/pandas/core/generic.py
--- a/pandas/core/generic.py
+++ b/pandas/core/generic.py
@@ -482,6 +482,11 @@ def pct_change(self, periods=1, fill_method='pad', limit=None, freq=None,
             np.putmask(rs.values, mask, np.nan)
         return rs
 
+    def to_hdf(self, path_or_buf, key, **kwargs):
+        """ activate the HDFStore """
+        from pandas.io import pytables
+        return pytables.to_hdf(path_or_buf, key, self, **kwargs)
+
 # install the indexerse
 for _name, _indexer in indexing.get_indexers_list():
     PandasObject._create_indexer(_name,_indexer)
diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py
--- a/pandas/io/pytables.py
+++ b/pandas/io/pytables.py
@@ -147,6 +147,27 @@ def get_store(path, mode='a', complevel=None, complib=None,
             store.close()
 
 
+### interface to/from ###
+
+def to_hdf(path_or_buf, key, value, mode=None, complevel=None, complib=None, **kwargs):
+    """ store this object, close it if we opened it """
+    f = lambda store: store.put(key, value, **kwargs)
+
+    if isinstance(path_or_buf, basestring):
+        with get_store(path_or_buf, mode=mode, complevel=complevel, complib=complib) as store:
+            f(store)
+    else:
+        f(path_or_buf)
+
+def read_hdf(path_or_buf, key, **kwargs):
+    """ read from the store, closeit if we opened it """
+    f = lambda store: store.select(key, **kwargs)
+
+    if isinstance(path_or_buf, basestring):
+        with get_store(path_or_buf) as store:
+            return f(store)
+    f(path_or_buf)
+    
 class HDFStore(object):
     """
     dict-like IO interface for storing pandas objects in PyTables
@@ -190,7 +211,7 @@ class HDFStore(object):
     """
     _quiet = False
 
-    def __init__(self, path, mode='a', complevel=None, complib=None,
+    def __init__(self, path, mode=None, complevel=None, complib=None,
                  fletcher32=False):
         try:
             import tables as _
@@ -198,6 +219,8 @@ def __init__(self, path, mode='a', complevel=None, complib=None,
             raise Exception('HDFStore requires PyTables')
 
         self._path = path
+        if mode is None:
+            mode = 'a'
         self._mode = mode
         self._handle = None
         self._complevel = complevel


Test patch:
diff --git a/pandas/io/tests/test_pytables.py b/pandas/io/tests/test_pytables.py
--- a/pandas/io/tests/test_pytables.py
+++ b/pandas/io/tests/test_pytables.py
@@ -94,6 +94,37 @@ def test_factory_fun(self):
         finally:
             safe_remove(self.path)
 
+    def test_conv_read_write(self):
+
+        try:
+
+            from pandas import read_hdf
+        
+            def roundtrip(key, obj,**kwargs):
+                obj.to_hdf(self.path, key,**kwargs)
+                return read_hdf(self.path, key)
+            
+            o = tm.makeTimeSeries()
+            assert_series_equal(o, roundtrip('series',o))
+            
+            o = tm.makeStringSeries()
+            assert_series_equal(o, roundtrip('string_series',o))
+            
+            o = tm.makeDataFrame()
+            assert_frame_equal(o, roundtrip('frame',o))
+
+            o = tm.makePanel()
+            tm.assert_panel_equal(o, roundtrip('panel',o))
+
+            # table
+            df = DataFrame(dict(A=range(5), B=range(5)))
+            df.to_hdf(self.path,'table',append=True)
+            result = read_hdf(self.path, 'table', where = ['index>2'])
+            assert_frame_equal(df[df.index>2],result)
+
+        finally:
+            safe_remove(self.path)
+
     def test_keys(self):
 
         with ensure_clean(self.path) as store: