如何解决 windows 系统上的内存错误

How to resolve a memory error on windows system

我正在合并几个相当大的数据帧:

 path ='D:/filename.csv'
 dfa=pd.read_csv(path, header=0, delimiter=',',encoding='latin-1', dtype=str) 
 dfa=dfa.rename(columns={"ybvd": "yBvD"})
 df=df.merge( dfa, on=['yBvD'], copy=True, how='left')

我收到以下错误:

MemoryError: Unable to allocate 11.8 GiB for an array with shape (1577604547,) and data type int64

我不太明白为什么,尤其是因为每次都不会发生相同的合并过程(我合并了几个数据集,代码代表了我每次使用的内容)。代码在任何时候都不会使用超过 60% 的 RAM(256GB 总 RAM 容量)。我还剩下 800GB 的虚拟内存。我要写入文件的硬盘上有很多space。

因此我不太确定这个错误应该向我指示什么或如何解决它。

MemoryError: Unable to allocate 11.8 GiB for an array with shape (1577604547,) and data type int64

这意味着您的 RAM space 内存不足。

At no point does the code use more than 60% of my RAM

pandas 是说它确实需要 11.8 GiB,请不要考虑使用了多少 % RAM,而是还有多少 GiB 仍然可用。

how to resolve it.

如果您无法获得足够的 RAM space,那么您会想办法在不一次加载所有数据的情况下进行操作,请参阅 Scaling to large datasets 以获得一些建议。

并使用批处理方法:

代码:

from tqdm.notebook import tqdm

def part_merge(part):
    return df2.merge(part, on='test')
edf = pd.DataFrame()
nss = 10
step = int(len(df1)/nss)
for i in tqdm(range(nss)):
    temp = df1.iloc[i*step:(i+1)*step]
    edf = edf.append(part_merge(temp))