OverflowError: Python int too large to convert to C long[Solved]

Python int is a data type that represents an integer. An integer is a whole number, such as 0, 19, or -21.

The int data type represents whole numbers in Python and is the default data type for representing integers.

You can also create an int from a string by using the int() function. The int() function takes a string as its argument and returns the integer representation of the string.

What is OverflowError in Python?

Python OverflowError is an error that occurs when a mathematical operation or assignment exceeds the maximum value that a numeric type can hold. OverflowError is raised when an operation or assignment produces a result too large to be represented by the numeric type.

Here’s a quick example of how you can encounter an OverflowError in Python.

data = 2 ** 999

In the above example, we attempt to assign the 2 ** 999 to the data variable. However, the 2 ** 999 is too large to be represented by a Python int, so the Python interpreter will raise an OverflowError.

How OverflowError occurs in Python?

The OverflowError: Python int too large too convert to C long occurs when an integer value is too large to represent a long in C. This error can occur when you try to convert a large integer value to a long using the ctypes module.

The ctypes module allows you to call functions from a shared library or DLL using the foreign function interface (FFI). When calling a function from a shared library or DLL, the ctypes module converts Python data types to the corresponding C data types. For example, a Python int is converted to a C long when calling a C function.

However, if the Python int is too large to be represented as a C long, the ctypes module will raise an OverflowError when converting it.

OverflowError in Pandas

Python OverflowError also occurs when we use the pandas library to store int values. The int number has some end limit to which several int data can be stored. If the limit is exceeded, then an OverflowError occurs.

There are different types of OverFlowError. Some of them can be math in range error, the result too large error, and Python int too large to convert to c long.

Example

import time

current = [t for t in time.localtime()] 
print(" The current data and time: ", current) 
t2 = (2**99)
print(" The invalid time is: ", t2)
current [3] = t2
t3 = time.asctime(tuple(current))
print(t3)

Output

OverflowError: Python int too large to convert to C long

In this example, an OverflowError occurs while assigning t3, which is Python int too large to convert to c long error. This is because the number exceeds the int range. This can be handled using try and except block.

How to Solve Python int too large to convert to C OverflowError?

To solve Python int too large to convert to c long OverflowError, convert an integer to int64 or use other types to store the values. Ensure that this number must be less than the int range. If it exceeds the int range, it will throw an OverflowError.

We can check whether the number is within the int range in the try block. If it exceeds, then the exception block is executed.

import time

try:
  current = [t for t in time.localtime()]
  print(" The current data and time: ", current)
  t2 = (2**99)
  print(" The invalid time is: ", t2)
  current[3] = t2
  t3 = time.asctime(tuple(current))
  print(t3)

except OverflowError as e:
  print("The error name is: ", e)

Output

The current data and time: [2022, 4, 7, 14, 3, 59, 3, 97, 0]
The invalid time is: 633825300114114700748351602688
The error name is: Python int too large to convert to C long

If we execute this program, the error message is displayed as:

The error name is: Python int too large to convert to c long

The OverflowError raises when you work with Pandas library. For example, the interpreter throws this error while storing a number greater than the int limit.

import pandas as pd 

df = pd.DataFrame({'Column_1': ['23845687891985972', '9823586793475672']})
df['int'] = df['Column_1'].astype('int') 
print(df['int'])

The OverflowError is raised because we convert the long number into an int. We can solve this problem by changing the astype to int64. But first, we can try if int64 can store that value. If not, we have to store it in another data type.

import pandas as pd 

df = pd.DataFrame({'Column_1': ['23845687891985972', '9823586793475672']})
df['int'] = df['Column_1'].astype('int64') 
print(df['int'])

Output

0 23845687891985972
1 9823586793475672

Name: int, dtype: int64

We can store this number in the int64 data type to resolve the error.

We can store any integer using the following command to get the maximum size.

import sys

print(sys.maxsize)

Output

9223372036854775807

You can see that it returns the maximum integer size for my computer. It is different from computer to computer. The number to be converted can be compared with this maximum size.

Conclusion

The OverflowError is raised when a mathematical operation or assignment exceeds the maximum value a numeric type can hold in Python.  Solve the error by converting the number to int64 instead of int. Another way is by using a try-except block.

That’s it for this tutorial.

See also

How to Solve ArithmeticError Exception in Python

TypeError: list indices must be integers or slices, not str in Python

ZeroDivisionError: division by zero in Python

Leave a Comment