In this article, we will deal with Typeerror cannot convert the series to class int.
We will also provide example codes of how this error occurs and solutions to help you solve the error quickly.
But before that let’s understand first what kind of error this is.
What is Typeerror cannot convert the series to class int?
The Python “TypeError: cannot convert the series to class int” occurs when we are trying to convert a Series object to an integer data type.
Here is an example error:
import pandas as pd
data = {
'name': ['I', 'T', 'S'],
'height': [11.5, 12.5, 13.5],
}
df = pd.DataFrame(data)
df['height'] = int(df['height'])
Result:
TypeError: cannot convert the series to <class 'int'>
Why this Typeerror occur?
Mainly the reason why we get this error is the series object is not properly typecast.
Since the panda package allows us to convert existing data into dataframe. Hence, we can now easily manipulate the columns in various built-in functions.
Particularly we can select any of the columns and convert it into series through the square brackets just like this — [“your column”].
Now that you already know that any of the columns you select has their values in series. Therefore every time you typecast the series with the use of int function definitely it will not work.
Additionally, most of the coders do convert data type values of the column into int but then it is not properly typecast. As a result, TypeError: cannot convert the series to class int is thrown.
How this error Typeerror cannot convert the series to class int occur?
Normally, the series object is a one-dimensional array mostly used by pandas for Dataframe columns.
Assuming we will work the pandas library and creates a DataFrame as shown below:
import pandas as pd
df = pd.DataFrame({"height": [4.6, 17.4, 19.6, 24.3]})
print(df)
Result:
height 0 4.6 1 17.4 2 19.6 3 24.3
As you noticed, DataFrame has only one column with float values. This time we will convert the float values into int.
Now we created the new Dataframe column and called the int function in order to convert height column values:
import pandas as pd
df = pd.DataFrame({"height": [4.6, 17.4, 19.6, 24.3]})
df['int_height'] = int(df['height'])
Output:
Traceback (most recent call last): File "C:\Users\Windows\PycharmProjects\pythonProject\main.py", line 5, in <module> df['int_height'] = int(df['height']) File "C:\Users\Windows\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\series.py", line 191, in wrapper raise TypeError(f"cannot convert the series to {converter}") TypeError: cannot convert the series to <class 'int'>
Apparently, the int() function in python only converts a single value into its integers equivalent.
Therefore the error occurs because we are using a function to convert a series wherein it consists of multiple float values.
Definitely, it doesn’t matter if we have a series of strings, floats, or any other type. Importantly, int() function can’t work with a series.
How to fix Typeerror cannot convert the series to class int?
Here are the ways to fix Typeerror cannot convert the series to class int.
Use the astype() function
Pandas consist of astype() function. This is used to convert a series into other types.
If we use this function and call the .astype(int) function, this will convert column values without triggering the error.
Look at the example program as follows:
import pandas as pd
df = pd.DataFrame({"height": [4.6, 17.4, 19.6, 24.3]})
df['integer_height'] = df['height'].astype(int)
print(df)
Output:
height integer_height 0 4.6 4 1 17.4 17 2 19.6 19 3 24.3 24
Use the apply() function
The other way to fix the Typeerror cannot convert the series to class int is using the apply() function.
Technically apply() function repeat over the column from which you call the function. Hence, it applies the function that passed as its arguments.
Furthermore, when the int function passed into apply function, the pandas will call the int() in each value of the series.
Here is the example code on how it works:
import pandas as pd
df = pd.DataFrame({"height": [4.6, 17.4, 19.6, 24.3]})
df['integer_height'] = df['height'].apply(int)
print(df)
Output:
height integer_height 0 4.6 4 1 17.4 17 2 19.6 19 3 24.3 24
Apart from that, the apply() function also works in converting a series of timestamps:
Here is the example code:
import pandas as pd
from datetime import datetime
df = pd.DataFrame(
{"timestamp": [1694196310, 1694196140, 1674196620, 1684195420]}
)
df['DATE'] = df['timestamp'].apply(datetime.fromtimestamp)
print(df)
Output:
timestamp DATE 0 1694196310 2023-09-09 02:05:10 1 1694196140 2023-09-09 02:02:20 2 1674196620 2023-01-20 14:37:00 3 1684195420 2023-05-16 08:03:40
Use the list comprehension syntax
The additional way to fix the error Typeerror cannot convert the series to class int, is to use list comprehension syntax.
The apply() function this time works as a list comprehension, this will convert values in series one by one.
Here is the example code:
import pandas as pd
df = pd.DataFrame({"height": [4.6, 17.4, 19.6, 24.3]})
df['integer_height'] = [int(v) for v in df['height']]
print(df)
The example code above demonstrates a list comprehension syntax. This will call the int() function for each value in the height column, similar to the apply() function.
Output:
height integer_height 0 4.6 4 1 17.4 17 2 19.6 19 3 24.3 24
Conclusion
In conclusion, we already now know the conversion of series values into integer type.
Particularly, python interpreter will throw an error when we pass all the column values to the int function. Since it only works on a single variable value.
By following the given solutions above, then it will solve the error you are getting.
Anyhow, if you are finding solutions to some errors you might encounter we also have TypeError can’t concat str to bytes.