[Python] Writing complex Pandas DataFrame to HDF5 using h5py

Discussão em 'Python' iniciado por Stack, Março 3, 2025.

  1. Stack

    Stack Membro Participativo

    I have a Pandas DataFrame with mixed scalar and array-like data of different raw types (int, float, str). The DataFrame's types look like this:

    'col1', dtype('float64')
    'col2', dtype('O') <-- array, item type str
    'col3', dtype('int64'
    'col4', dtype('bool')
    ...
    'colA', dtype('O') <-- array, item type str
    ...
    'colB', dtype('O') <-- array, item type float
    ...
    'colC', dtype('O') <-- scalar, str
    ...
    'colD', dtype('O') <-- array, item type int
    ...
    some more mixed data type columns


    The length of the numeric array-like data is variable fron DataFrame row to row.

    Currently, I try to write this DataFrame naively to a HDF5 file by

    with h5py.File(self.file_path, 'w') as f:
    f.create_dataset(dataset_name, data=dataframe)


    This operation fails with the error message

    tid = h5t.py_create(dtype, logical=1)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "h5py/h5t.pyx", line 1669, in h5py.h5t.py_create
    File "h5py/h5t.pyx", line 1693, in h5py.h5t.py_create
    File "h5py/h5t.pyx", line 1753, in h5py.h5t.py_create
    TypeError: Object dtype dtype('O') has no native HDF5 equivalent


    What is a way / the best way to write this data to HDF5?

    Continue reading...

Compartilhe esta Página