"OverflowError: Python int too large to convert to C long" on windows but not mac

"OverflowError: Python int too large to convert to C long" on windows but not mac

我 运行 windows 和 mac 上的代码完全相同,python 3.5 64 位。

在 windows 上,它看起来像这样:

>>> import numpy as np
>>> preds = np.zeros((1, 3), dtype=int)
>>> p = [6802256107, 5017549029, 3745804973]
>>> preds[0] = p
Traceback (most recent call last):
  File "<pyshell#13>", line 1, in <module>
    preds[0] = p
OverflowError: Python int too large to convert to C long

但是,此代码在我的 mac 上运行良好。任何人都可以帮助解释原因或为 windows 上的代码提供解决方案吗?非常感谢!

一旦您的数字大于 sys.maxsize:

,您就会收到该错误消息
>>> p = [sys.maxsize]
>>> preds[0] = p
>>> p = [sys.maxsize+1]
>>> preds[0] = p
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: Python int too large to convert to C long

您可以通过检查确认这一点:

>>> import sys
>>> sys.maxsize
2147483647

要获取更高精度的数字,请不要传递在幕后使用有界 C 整数的 int 类型。使用默认浮点数:

>>> preds = np.zeros((1, 3))

您可以使用 dtype=np.int64 而不是 dtype=int

Could anyone help explain why

Numpy 数组通常*具有固定大小的元素,包括各种大小的整数、单精度或双精度浮点数、固定长度的字节和 Unicode 字符串以及由上述类型构建的结构。

在 Python 2 中,一个 python "int" 相当于一个 C long。在 Python 3 中,“int”是任意精度类型,但 numpy 在创建数组时仍然使用“int”来表示 C 类型“long”。

C long 的大小取决于平台。在 windows 上,它始终是 32 位的。在类 unix 系统上,它通常在 32 位系统上为 32 位,在 64 位系统上为 64 位。

or give a solution for the code on windows? Thanks so much!

选择大小与平台无关的数据类型。您可以在 https://docs.scipy.org/doc/numpy/reference/arrays.scalars.html#arrays-scalars-built-in 找到列表,最明智的选择可能是 np.int64

* Numpy 确实允许 python 个对象的数组,但我不认为它们被广泛使用。

转换为浮点数:

import pandas as pd

df = pd.DataFrame()
l_var_l = [8258255190131389999999000003296, 50661]
df['temp'] = l_var_l
df['temp'] = df['temp'].astype(int)

以上失败并出现错误:

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

现在试试浮点数转换:

df['temp'] = df['temp'].astype(float)