This error occurs if you try to call isna()
on a numpy.float64
object. If you want to evaluate whether a value is NaN
or not in a Series or DataFrame object, you use can use the Series.isna()
and DataFrame.isna()
methods respectively. For example,
import pandas as pd import numpy as np ser = pd.Series([np.nan, np.nan, 3, 4 , 7]) ser_2 = np.where(ser.isna()==False, ser, 0)
You can also use Series.isnull() and DataFrame.isnull() which are aliases for the isna() methods.
This tutorial will go through the error in detail and how to solve it with code examples.
Table of contents
How to Solve AttributeError: ‘numpy.float64’ object has no attribute ‘isna’
AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. The part “‘numpy.float64’ object has no attribute ‘isna’” tells us that the numpy.float64
object we are handling does not have the isna attribute. isna()
is a pandas DataFrame and Series method which returns a boolean object the same size as the original, indicating which values are NA
.
Example
Let’s look at an example to reproduce the error.
# Import Pandas and NumPy libraries import pandas as pd import numpy as np # Create Series object ser = pd.Series([np.nan, np.nan, 3, 4 , 7]) # For loop over elements in Series for ele in ser: # Check if element is NaN if ele.isna(): # Replace NaN with 0 ser = ser.replace(ele,0) # Print 0 print(ser)
Let’s run the code to see what happens:
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Input In [19], in <cell line: 6>() 4 ser = pd.Series([np.nan, np.nan, 3, 4 , 7]) 6 for ele in ser: ----> 7 if ele.isna(): 8 ser = ser.replace(ele,0) 10 print(ser) AttributeError: 'float' object has no attribute 'isna'
The error occurs because we are trying to call the isna()
method on the elements in the Series object, which are numpy.float64
.
Solution
We can solve the error by using the Series method isna(). Let’s look at the revised code:
# Import Pandas and NumPy libraries import pandas as pd import numpy as np # Create Series object ser = pd.Series([np.nan, np.nan, 3, 4 , 7]) # Create numpy array using where function arr = np.where(ser.isna()==False, ser, 0) # Convert array to Series ser_2 = pd.Series(arr) # Print Series object print(ser_2) # Confirm type of object print(type(ser_2))
The numpy where method returns the element from the original series if the condition ser.isna()==False
otherwise it returns 0
. Let’s run the code to get the result:
0 0.0 1 0.0 2 3.0 3 4.0 4 7.0 dtype: float64 <class 'pandas.core.series.Series'>
We successfully retrieved a Series object derived from the original, where the NaN
values are replaced by 0
and the non NaN
are left unchanged.
Summary
Congratulations on reading to the end of this tutorial!
For further reading on AttributeErrors, go to the article;
How to Solve Python AttributeError: ‘Series’ object has no attribute ‘lower’
Go to the online courses page on Python to learn more about coding in Python for data science and machine learning.
Have fun and happy researching!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.