【问题标题】:How to keep hdf5 binary of a pandas dataframe in-memory?如何将 pandas 数据帧的 hdf5 二进制文件保存在内存中?
【发布时间】:2021-08-01 22:29:06
【问题描述】:

我想获取导出为 hdf5 的 pandas 数据帧的字节内容,理想情况下不实际保存文件(即,在内存中)。

python>=3.6, < 3.9(和pandas==1.2.4pytables==3.6.1)上,以下曾经工作:

import pandas as pd
with pd.HDFStore(
    "in-memory-save-file",
    mode="w",
    driver="H5FD_CORE",
    driver_core_backing_store=0,
) as store:
    store.put("my_key", df, format="table")
    binary_data = store._handle.get_file_image()

其中df是要转成hdf5的dataframe,最后一行调用this pytables function

但是,从 python 3.9 开始,使用上面的 sn-p 时出现以下错误:

File "tables/hdf5extension.pyx", line 523, in tables.hdf5extension.File.get_file_image
tables.exceptions.HDF5ExtError: Unable to retrieve the size of the buffer for the file image.  Plese note that not all drivers provide support for image files.

错误是由上面链接的同一 pytables 函数引发的,显然是由于 retrieving the size of the buffer for the file image 时的问题。不过,我不明白它的最终原因。

我已经尝试了其他替代方法,例如保存到 BytesIO file-object,但目前未成功。

如何在 python 3.9 上将 pandas 数据帧的 hdf5 二进制文件保存在内存中?

【问题讨论】:

  • This 是 github 上的一个相关(尽管是旧的)讨论,建议调用 get_file_image()

标签: python pandas hdf5 pytables python-3.9


【解决方案1】:

解决方法是使用conda install -c conda-forge pytables 而不是pip install pytables。不过,我仍然不明白错误背后的最终原因。

【讨论】:

    猜你喜欢
    • 2020-04-14
    • 2017-12-13
    • 2012-08-29
    • 1970-01-01
    • 2010-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多