【问题标题】:How do I get conversion with "OverflowError: Python int too large to convert to C long" error working on Windows Server 2012?如何在 Windows Server 2012 上使用“OverflowError:Python int too large to convert to C long”错误进行转换?
【发布时间】:2026-01-30 13:30:01
【问题描述】:

在 Anaconda Python 2.7.12、Pandas 18.1、Windows Server 2012 上运行时:

df['z'] = df['y'].str.replace(' ', '').astype(int)

我收到此错误:

OverflowError: Python int too large to convert to C long

我在 MacOS 10.11 或 Ubuntu 14.04 上没有收到此错误。我从其他地方读到,Windows C++ 编译器对 long 的定义与类 Unix 操作系统不同。如果是这样,我该如何在 Windows 上进行这项工作?

此外,data.txt 的大小仅为 172 KB。如果有帮助,data.txt 会采用这种形式:

x|y
99999917|099999927 9991
99999911|999999979 9994
99999912|999999902 9992

【问题讨论】:

  • 试试 .astype('int64') - 在 Windows 上 int 总是 32 位。
  • 那行得通。将此作为答案,我会接受。

标签: python c++ pandas numpy anaconda


【解决方案1】:

int 被 numpy by 解释为 np.int_ dtype,对应于 C 整数。在 Windows 上,即使在 64 位系统上,这也是一个 32 位整数。

因此,如果您需要转换更大的值,请使用 64 位整数指定

.astype('int64')

【讨论】:

    最近更新 更多