【发布时间】:2017-12-22 16:08:16
【问题描述】:
这里是 Python 新手。
我正在尝试使用 to_hdf 将大型数据帧保存到具有 lz4 压缩的 HDF 文件中。
我使用 Windows 10、Python 3、Pandas 20.2
我收到错误“OverflowError: Python int too large to convert to 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
【问题讨论】:
-
您确实期望
999999999999999999的整数值吗?或者这只是一个坏例子?如果是前者,使用浮点值会损害精度吗? -
“以前的帖子讨论了 dtype”:这个问题也与 dtype 有关,因为这些整数值太大而无法被 4 字节整数容纳。您可能想要显示数据框的 dtype。
-
感谢 Evert 的评论。该示例旨在说明它与整数值或数据类型无关。有 500M 个相同的行。少写一个 300M 行的文件就可以了。 400M 失败。