Skip to main content

Python NumPy: How to Resolve "OverflowError: Python int too large to convert to C long"

When working with NumPy, you might encounter the OverflowError: Python int too large to convert to C long. This error occurs when you try to create a NumPy array with a Python integer that exceeds the maximum value allowed by the underlying C data type that NumPy is using. Unlike Python's standard int type, which can handle arbitrarily large numbers, NumPy's integer types have fixed sizes for performance reasons.

This guide will explain the fundamental difference between Python and NumPy integers, show you how to reproduce the error, and provide the correct solutions by either specifying a larger data type or using a more flexible object array.

Understanding the Error: Python int vs. NumPy int

The root of this error is the difference in how Python and NumPy handle integers:

  • Python int: In Python 3, integers have arbitrary precision. This means they can grow as large as your computer's memory allows.
  • NumPy int: For performance, NumPy's integer types (np.int32, np.int64, etc.) are fixed-size and map directly to integer types in the C programming language. This means they have a strict minimum and maximum value they can store.

The OverflowError happens when NumPy tries to fit a large Python int into a fixed-size C long (or long long) and finds that it is too big.

note

On a typical 64-bit system:

  • A C long (and np.int64) can hold values up to 9,223,372,036,854,775,807 (or 2**63 - 1).
  • An unsigned C unsigned long long (and np.uint64) can hold values up to 18,446,744,073,709,551,615 (or 2**64 - 1).

Reproducing the OverflowError

Let's try to create a NumPy array with a number that is larger than the maximum value for a standard 64-bit signed integer.

Example of code causing the error:

import numpy as np

# This number is 2**63, one larger than the max for a signed 64-bit integer.
large_number = 9223372036854775808

# By default on a 64-bit system, NumPy tries to use a 64-bit integer.
arr = np.array([large_number])

Output:

Traceback (most recent call last):
File "main.py", line 7, in <module>
OverflowError: Python int too large to convert to C long

Solution 1: Specify a Larger NumPy Data Type (e.g., np.int64 or object)

If the number fits within a 64-bit integer but NumPy is defaulting to a smaller type (common on 32-bit systems), you can explicitly set the dtype. If the number is too large for any fixed-size integer type, you must use dtype=object.

Solution:

import numpy as np

# This number is larger than a signed 64-bit int, but fits in an unsigned one.
large_positive_number = 9223372036854775808

# For truly massive numbers that won't fit in any standard C type
massive_number = 9223372036854775808 * 1000

# ✅ Solution A: Explicitly use np.int64 (if it fits)
# This would fix the error on a 32-bit system if the number was smaller.
# arr_64 = np.array([2147483648], dtype=np.int64)

# ✅ Solution B: Use dtype=object for arbitrarily large integers.
# This tells NumPy to store a pointer to the Python int object.
object_array = np.array([massive_number], dtype=object)

print(f"Array with massive number (dtype=object): {object_array}")
print(f"Type of element in object array: {type(object_array[0])}")

Output:

Array with massive number (dtype=object): [9223372036854775808000]
Type of element in object array: <class 'int'>
warning

Using dtype=object solves the overflow, but it comes at a significant performance cost. NumPy operations on an object array will be much slower because it can no longer use its highly optimized C and Fortran routines. Use this only when absolutely necessary.

Solution 2: Use Unsigned Integers for Large Positive Numbers (np.uint64)

If your number is positive and larger than the maximum signed integer (2**63 - 1) but smaller than the maximum unsigned integer (2**64 - 1), you can use an unsigned integer dtype.

Solution:

import numpy as np

# This number is 2**63. It's too large for np.int64 but fits in np.uint64.
large_positive_number = 9223372036854775808

# ✅ Specify the unsigned 64-bit integer type
unsigned_array = np.array([large_positive_number], dtype=np.uint64)

print(f"Array with large positive number (dtype=np.uint64): {unsigned_array}")

Output:

Array with large positive number (dtype=np.uint64): [9223372036854775808]

How to Check Maximum Integer Sizes

You can programmatically check the limits of NumPy's data types using the numpy.iinfo function.

Solution:

import numpy as np

# Get info about the signed 64-bit integer type
info_int64 = np.iinfo(np.int64)
print(f"np.int64 Max Value: {info_int64.max}")

# Get info about the unsigned 64-bit integer type
info_uint64 = np.iinfo(np.uint64)
print(f"np.uint64 Max Value: {info_uint64.max}")

Output:

np.int64 Max Value: 9223372036854775807
np.uint64 Max Value: 18446744073709551615

Conclusion

If your integer...The best solution is...
Exceeds the default int size (on 32-bit systems) but fits in a 64-bit int.Specify dtype=np.int64.
Is positive and exceeds the np.int64 limit but fits in an np.uint64.Specify dtype=np.uint64.
Is too large for any fixed-size dtype (e.g., larger than 2**64 - 1).Specify dtype=object, but be aware of the performance impact.

The OverflowError: Python int too large to convert to C long is a direct result of the design trade-offs between Python's flexible integers and NumPy's high-performance, fixed-size arrays. By explicitly managing the dtype, you can correctly handle large numbers in your numerical computations.