diff --git a/doc/source/user_guide/cookbook.rst b/doc/source/user_guide/cookbook.rst index 56ef6fc479f2c..50b946999092a 100644 --- a/doc/source/user_guide/cookbook.rst +++ b/doc/source/user_guide/cookbook.rst @@ -1166,6 +1166,25 @@ Storing Attributes to a group node store.close() os.remove('test.h5') +You can create or load a HDFStore in-memory by passing the ``driver`` +parameter to PyTables. Changes are only written to disk when the HDFStore +is closed. + +.. ipython:: python + + store = pd.HDFStore('test.h5', 'w', diver='H5FD_CORE') + + df = pd.DataFrame(np.random.randn(8, 3)) + store['test'] = df + + # only after closing the store, data is written to disk: + store.close() + +.. ipython:: python + :suppress: + + os.remove('test.h5') + .. _cookbook.binary: Binary files diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index 8aac8f9531512..800e9474cc0f8 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -447,8 +447,8 @@ class HDFStore: Parameters ---------- - path : string - File path to HDF5 file + path : str + File path to HDF5 file. mode : {'a', 'w', 'r', 'r+'}, default 'a' ``'r'`` @@ -462,18 +462,20 @@ class HDFStore: ``'r+'`` It is similar to ``'a'``, but the file must already exist. complevel : int, 0-9, default None - Specifies a compression level for data. - A value of 0 or None disables compression. + Specifies a compression level for data. + A value of 0 or None disables compression. complib : {'zlib', 'lzo', 'bzip2', 'blosc'}, default 'zlib' - Specifies the compression library to be used. - As of v0.20.2 these additional compressors for Blosc are supported - (default if no compressor specified: 'blosc:blosclz'): - {'blosc:blosclz', 'blosc:lz4', 'blosc:lz4hc', 'blosc:snappy', - 'blosc:zlib', 'blosc:zstd'}. - Specifying a compression library which is not available issues - a ValueError. + Specifies the compression library to be used. + As of v0.20.2 these additional compressors for Blosc are supported + (default if no compressor specified: 'blosc:blosclz'): + {'blosc:blosclz', 'blosc:lz4', 'blosc:lz4hc', 'blosc:snappy', + 'blosc:zlib', 'blosc:zstd'}. + Specifying a compression library which is not available issues + a ValueError. fletcher32 : bool, default False - If applying compression use the fletcher32 checksum + If applying compression use the fletcher32 checksum. + **kwargs + These parameters will be passed to the PyTables open_file method. Examples -------- @@ -482,6 +484,17 @@ class HDFStore: >>> store['foo'] = bar # write to HDF5 >>> bar = store['foo'] # retrieve >>> store.close() + + **Create or load HDF5 file in-memory** + + When passing the `driver` option to the PyTables open_file method through + **kwargs, the HDF5 file is loaded or created in-memory and will only be + written when closed: + + >>> bar = pd.DataFrame(np.random.randn(10, 4)) + >>> store = pd.HDFStore('test.h5', driver='H5FD_CORE') + >>> store['foo'] = bar + >>> store.close() # only now, data is written to disk """ _handle: Optional["File"] @@ -634,6 +647,8 @@ def open(self, mode: str = "a", **kwargs): ---------- mode : {'a', 'w', 'r', 'r+'}, default 'a' See HDFStore docstring or tables.open_file for info about modes + **kwargs + These parameters will be passed to the PyTables open_file method. """ tables = _tables()