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

Contents

## Introduction

Welcome to another tutorial on python. In today’s article, we will learn something new and different. Making our way through a detailed python Exception handling series brings us today to OverflowError.

OverflowError: Python int too large to convert to C long is a typical error in python which occurs when you initialize too large integer. Currently, the maximum capacity of an integer is limited to `sys.maxint`. If you initialize the integers greater than it, an error will be raised. Let’s dive deeper to get the concepts clear about OverflowError.

## What is OverflowError in Python?

Just like other programming languages, the OverflowError in python is raised when the result of an arithmetic operation is out of range. By this, we mean that when an arithmetic operation exceeds the limit of the variable type, we get an overflow error exception.

Let us go to the code section to understand OverflowError.

```import math
e = 250000
m = math.exp(e)
print(m)
```

OUTPUT:-

```Traceback (most recent call last):
File "main.py", line 3, in
m = math.exp(e)
OverflowError: math range error```

Explanation of the code

In the above program of python, we can see that we are importing the math module. We are using the math module to calculate the exponential value, such as exp(10000). In mathematical terms, it means e10000, and the value of e is 2.7.

Thus we land up to give up the statement as 2.710000. However, when we are trying to calculate this, it will give the value as a result that is double and cannot print it. Therefore, it gives us an overflow exception. That is why we get it is out of range.

### There are many types of OverflowError such as :

1. OverflowError: math in range error.
2. OverflowError: Python int too large to convert to C long.
3. The result too large.

But in this tutorial, we will specifically deal with “OverflowError: Python int too large to convert to C long.”

## What is OverflowError: Python int too large to convert to C long?

This error occurs when data type value storage exceeds the range. Now let us demonstrate this value exceeds in data type values in the below example:

```import time
current_time = [tm for tm in time.localtime()]
print("The current data and time:",current_time)

time2 = (2**99)
print("The invalid time is:",time2)

current_time = time2
time3 = time.asctime(current_time)
print(time3)
```

OUTPUT:-

```The current data and time: [2021, 5, 18, 19, 1, 10, 1, 138, 0]
The invlaid time is: 618970019642690137449562112
Traceback (most recent call last):
File "D:\c prog\xx.py", line 9, in
time3 = time.asctime(current_time)
OverflowError: Python int too large to convert to C long```

Explanation of the code

1. In the above program, we are printing the current time using the time module.
2. When we are printing the current time, we are printing the current time using time.localtime() function which results in the output with [year, month, day, minutes, seconds… ].
3. Then we are printing the value by changing the hours to a larger value limit it can store. Therefore when we are trying to print this value it will throw an error saying Python int is too large to convert to C long.
4. This output gives overflow error because the time3 uses plain integer object and therefore it cannot take objects of arbitrary length.

## How to resolve OverflowError: Python int too large to convert to C long?

In the above program, we saw the OverflowError that occurred when the current value exceeds the limit value. So to handle this, we have to raise the OverflowError exception. Now let us see how to resolve this problem. First, let’s implement the exception handling in our code.

```import time

try:
current_time = [tm for tm in time.localtime()]
print("The current data and time: ",current_time)

time2 = (2**99)
print("The invalid time is:",time2)

current_time = time2
print(time.asctime(current_time))

except OverflowError as of:
print("After the Overflow error", of)
```

Output:

```The current data and time: [2021, 5, 18, 19, 1, 10, 1, 138, 0]
The invlaid time is: 618970019642690137449562112
('After the Overflow error', OverflowError('Python int too large to convert to C long',))```

Explanation of the code:

In the above code, we saw that we handled the OverflowError by using try and except blocks or exception handling as we did in the above program.

## OverflowError: Python int too large to convert to C long in Pandas

Recently, while I was working with the panda’s module recently and I discovered an OverflowError: Python int too large to convert to C long. I was running a python script, where I have to convert a string column from a pandas df to int, using the `astype(int)` method. However, I got the error. The code was as follows:-

```import pandas as pd

df = pd.DataFrame({'t': ['123456789985', '178965423698']})
df['int'] = df['test'].astype('int')

print(df['int'])
```

OUTPUT:-

### Resolving the problem

All you need to do was pass int64 as the parameter in astype method.

```import pandas as pd

data_f = pd.DataFrame({'t': ['123456789985', '178965423698']})
data_f['int'] = data_f['test'].astype('int64')

print(data_f['int'])
```

OUTPUT:-

```123456789985
178965423698```

## Conclusion

In this article, we conclude that an overflow error is an error that occurs when the current runtime value obtained by the Python program exceeds the limit value. Also, we saw this error occurs when we use arithmetic operations in the program and the result exceeds the limit range value.

We also saw this error occurs when we are trying to convert from one data type to another when the value is larger than the given data type storage range. Lastly, we saw how to handle this error using exception handling i.e., try and except block.

If you come across any doubts feel free to comment down below. Till then keep pythoning geeks!

Subscribe
Notify of 