Fixing Python and Numpy AttributeError: ‘numpy.float64’ object has no attribute ‘log10’

Python and Numpy AttributeError: ‘numpy.float64’ object has no attribute ‘log10’ is an error which occurs when you use np.log10 the wrong way and might also happen for other reasons.

My goal today is to provide a clear and detailed explanation of why this error is happening and how to solve it, we will also check out other ways to get rid of this problem for good.

Exploring Python and Numpy AttributeError: ‘numpy.float64’ object has no attribute ‘log10’

This is an error which occurs when you use np.log10 the wrong way and might also happen for other reasons.

Please double check the error message in this blog post so you can avoid mixing between different python issues.

                                                                       #
AttributeError: 'numpy.float64' object has no attribute 'log10'
                                                                       #

Bellow we will describe how the error can be solved. With multiple possible solutions.

The Method that solved my problem : use a Python Lambda Function or use numpy.astype()

This is an error which occurs when you use np.log10 the wrong way. The error can also happen for other reasons. I will cover the options you can use to get rid of the error. Even if the causes behind the error can be different, the options I will present will solve all of them.

In our example, I am working with a Pandas Series instance named myseries, . myseries has object as its data type

                                                                       #
myseries.apply(np.log10)
                                                                       #

As a result, np.log10 wont be able to handle myseries which results in the error we are trying to solve.

The first option one can use is to use Python Lambda Functions, a Lambda function is an anonymous function here is how you can use it to solve this issue.

                                                                       #
myseries.apply(lambda x: np.log10(x))
                                                                       #

The second option is to use the numpy.astype() function to change the data type of myseries to float64, then we will apply np.log10 to that.

                                                                       #
myseries.astype(np.float64).apply(np.log10)
                                                                       #

There are many ways to do this but its just a matter of syntax, all the methods will serve the same purpose.

                                                                       #
np.log10(myseries.astype(np.float64))
                                                                       #

You can also achieve the same goal using this syntax.

                                                                       #
myseries.astype(np.float64)
                                                                       #

I believe, the error should be gone after you apply one of the options above. I wish you good luck with your python project and your python journey.

Summing-up : 

That’s it fellow developers, this is the end of this guide, I hope you found this useful in solving your particular issue, if you have the means and want to help, please support our work on our Kofi account.

Thank you for reading my blog post to the end, If you want to learn more about the Python programming language, check out the official Python Documentation : https://docs.python.org/3/