[Python] Write pandas DataFrame to HDF in memory buffer

Discussão em 'Python' iniciado por Stack, Agosto 16, 2025.

  1. Stack

    Stack Membro Participativo

    I want to get a dataframe as hdf in memory. The code below results in "AttributeError: '_io.BytesIO' object has no attribute 'put'". I am using python 3.5 and pandas 0.17

    import pandas as pd
    import numpy as np
    import io

    df = pd.DataFrame(np.arange(8).reshape(-1, 2), columns=['a', 'b'])
    buf = io.BytesIO()
    df.to_hdf(buf, 'some_key')


    Update: As UpSampler pointed out "path_or_buf" cannot be an io stream (which I find confusing since buf usually can be an io stream, see to_csv). Other than writing to disk and reading it back in, can I get a dataframe as hdf in memory?

    Continue reading...

Compartilhe esta Página