Are you getting the “TypeError: cannot convert the series to class float” error while working on pandas series in Python?
We all know that typecasting is a helpful feature of computer programming that allows us to convert the data type from one type to another. But these conversions are limited to some extent; we can convert integer to float, float to octal, and vice versa but not strings.
Computer programming doesn’t allow us to typecast a string into other data types because strings don’t have any mathematical properties to utilize in typecasting.
In this article, we’ll discuss what is the “TypeError: cannot convert the series to <class “float”>” and how to fix it, along with practical examples. So let’s get right into the topic.
Table of Contents
What is the “TypeError: Cannot Convert The Series to Class Float” Error in Python?
In Python, the “Type Error cannot convert the series to class float” error occurs when you try to convert a pandas series into a float, and it doesn’t allow typecasting to float. Before learning about series typecasting, first, let’s see how to do basic typecasting in Python.
Code
x = 10
print(f"The data type of {x} before typecasting is {type(x)}")
# typecast 'x' from int to 'float'
x = float(x)
print(f"The data type of {x} after typecasting is {type(x)}")
Output
The data type of 10 before typecasting is <class 'int'>
The data type of 10.0 after typecasting is <class 'float'>
As you can see, we have easily typecasted an int to a float; now let’s try some other data types.
Code
x = 342 # int
y = True # boolean
z = "String" # string
# typecast into Float
print(float(x))
print(float(y))
print(float(z)) # ValueError: could not convert string to float: 'String.'
Output
As you can see, the integer and boolean values are converted into Float, whereas the string has raised an error which indicates that we cannot convert a string into Float.
Now let’s see the pandas series:
Code
import pandas as pd
# create a pandas series
ser = pd.Series([1,2,3,4,5,6,7])
# print the data type of the series
print(type(ser))
# typecasting
print(float(ser))
Output
In the above example, we tried to convert the series into a float which isn’t allowed to do so; hence we got the TypeError: cannot convert the series to class float.
How to Fix the “TypeError: Cannot Convert The Series to Class Float” Error in Python?
We’ve seen that it isn’t allowed to typecast a string or a series into Float. The data type of series is < class’ pandas.core.series. Series’> cannot be converted into a float; instead, we can convert the data of it or use a specified function for it.
And for typecasting a pandas series we’ll use the astype() instead of the basic int(), Float (), str(), etc.
Syntax of astype() function:
pandas_series.astype( data_type )
Code
import pandas as pd
# create a pandas series
ser = pd.Series([1,2,3,4,5])
print("Before type casting")
print("************")
print(ser)
print("\n\nAfter type casting")
print("************")
# typecasting
print(ser.astype(float))
Output
Before type casting
************
0 1
1 2
2 3
3 4
4 5
dtype: int64
After type casting
************
0 1.0
1 2.0
2 3.0
3 4.0
4 5.0
dtype: float64
Perfect! We’ve successfully typecasted a pandas series from int to float using the astype() function.
Conclusion
To conclude this article on the TypeError: cannot convert the series to class float, we’ve discussed basic typecasting in Python. Furthermore, we’ve talked about how to typecast a pandas series in Python using the astype() function.
Brainstorming moment 💡, can we typecast some specific entities of a pandas series?