python - Pandas to_hdf 溢出错误

标签 python pandas hdf5 lz4

Python新手在这里。

我正在尝试使用 to_hdf 将大数据帧保存到带有 lz4 压缩的 HDF 文件中。

我使用 Windows 10、Python 3、Pandas 20.2

我收到错误“溢出错误:Python int 太大而无法转换为 C long”。

没有任何机器资源接近其极限(RAM、CPU、SWAP 使用)

以前的帖子讨论了 dtype,但下面的例子表明还有一些其他问题,可能与大小有关?

import numpy as np
import pandas as pd


# sample dataframe to be saved, pardon my French 
n=500*1000*1000
df= pd.DataFrame({'col1':[999999999999999999]*n,
                  'col2':['aaaaaaaaaaaaaaaaa']*n,
                  'col3':[999999999999999999]*n,
                  'col4':['aaaaaaaaaaaaaaaaa']*n,
                  'col5':[999999999999999999]*n,
                  'col6':['aaaaaaaaaaaaaaaaa']*n})

# works fine
lim=200*1000*1000
df[:lim].to_hdf('df.h5','table', complib= 'blosc:lz4', mode='w')

# works fine
lim=300*1000*1000
df[:lim].to_hdf('df.h5','table', complib= 'blosc:lz4', mode='w')


# Error
lim=400*1000*1000
df[:lim].to_hdf('df.h5','table', complib= 'blosc:lz4', mode='w')


....
OverflowError: Python int too large to convert to C long

最佳答案

我遇到了同样的问题,它似乎确实与数据框的大小有关,而不是与 dtype 相关(我将所有列都存储为字符串,并且能够将它们分别存储到 .h5)。
对我有用的解决方案是使用 mode='a' 以块的形式保存数据帧.
正如 pandas documentation 中所建议的:模式{‘a’,‘w’,‘r+’},默认‘a’: ‘a’:追加,打开现有文件进行读写,如果文件不存在则创建。
所以示例代码看起来像:

batch_size = 1000
for i, df_chunk in df.groupby(np.arange(df.shape[0]) // batch_size):
    df_chunk.to_hdf('df.h5','table', complib= 'blosc:lz4', mode='a')

关于python - Pandas to_hdf 溢出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45155782/

相关文章:

g++编译错误: undefined reference to a shared library function which exists

python - 将多个具有层次结构的 pd.DataFrame 保存到 hdf5

python - 在 Tkinter 中隐藏 TreeView 项目

python - 将两个时间序列与 tz 感知的日期时间索引结合起来

python - 如何从 python 数据框中获取过去几年的上周数据?

Python:导入双管 csv Pandas

python /Py2neo : weakref exception

python - 单击图例 python 散点图中的数据开/关

python - 有没有办法计算plotly Scatter3d的最佳sizeref值

python - 用pytables压缩数组