How to Solve OverflowError: Python int too large to convert to C long

To solve OverflowError: Python int too large to convert to C long, use the try-except statement or use the int64 as data type. If you use the try-except block, we can check whether the number is within the int range in the try block. If it exceeds, then the exception block will be executed. 

How OverflowError occurs in Python

The OverflowError: Python int too large too convert to C long occurs when working with large data sets. It occurs when we are using pandas library to store int values. The int number has some end limit up to which number 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.

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

To overcome this number must be less than the int range. If it is greater than the int range, we can convert it to int64 or use other types to store the values.

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, so the error is resolved.

To get the maximum size up to which we can store any integer, use the following command.

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 we convert the too-long number to an int number. We can 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