pandas.DataFrame.to_hdf

pandas 3.1.1 · DataFrame · method

Save pandas data under a key in an HDF5 store.

Call syntax

DataFrame.to_hdf(…) Full signature
DataFrame.to_hdf(
    path_or_buf,
    *,
    key,
    mode='a',
    complevel=None,
    complib=None,
    append=False,
    format=None,
    index=True,
    min_itemsize=None,
    nan_rep=None,
    dropna=…,
    data_columns=None,
    errors='strict',
    encoding='UTF-8',
)

Inspect the call

Run this code to inspect the callable and its parameters. Its data, backend, or environment is needed to execute the operation itself.

Parameters

ParameterRequirementAccepted values and behavior
path_or_buf Required
Positional or keyword
str or pandas.HDFStore

Supply a file path or writable/readable stream. Some writers return the result directly when this is omitted.

key Required
Keyword only
str

Provide the lookup key, or a transformation callable for a sorting method.

mode Optional
Keyword only
Default 'a'
{'a', 'w', 'r+'}

Choose a file or storage access mode.

complevel Optional
Keyword only
Default None
{0-9}
complib Optional
Keyword only
Default None
{'zlib', 'lzo', 'bzip2', 'blosc'}
append Optional
Keyword only
Default False
bool

Add records or keys to existing output instead of replacing it where supported.

format Optional
Keyword only
Default None
{'fixed', 'table', None}

Supply a format string or choose an output layout supported by this API.

index Optional
Keyword only
Default True
bool

Supply row labels or control how existing row labels are used.

min_itemsize Optional
Keyword only
Default None
dict or int
nan_rep Optional
Keyword only
Default None
str
dropna Optional
Keyword only
bool

Choose whether to drop missing values or missing keys from consideration.

data_columns Optional
Keyword only
Default None
list of columns or True
errors Optional
Keyword only
Default 'strict'
str

Choose how to handle invalid input or failed operations. The available options depend on the API.

encoding Optional
Keyword only
Default 'UTF-8'
str

Name a text encoding such as "utf-8".

Returns

None

Example for a local Python session

import pandas as pd
from pathlib import Path
from tempfile import TemporaryDirectory
import tables

with TemporaryDirectory() as folder:
    path = Path(folder) / "demo.h5"
    data = pd.DataFrame({"units": [2, 5]})
    data.to_hdf(path, key="orders")
    print(pd.read_hdf(path, "orders"))