'Float' object has no attribute 'log'

12,795

As pointed out by warren-weckesser this can also happen if you use dtype object (and in fact this is likelier the issue you are facing):

>>> s = pd.Series([1.0], dtype='object')
>>> s
0    1
dtype: object
>>> np.log(s)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'float' object has no attribute 'log'

You can address this by setting the dtype to float explicitly:

>>> np.log(s.astype('float64'))
0    0.0
dtype: float64

In your case:

np.log(df['price'].astype('float'))

Note: You can have more control using to_numeric.


First/alternative answer:

You have a float variable np in scope.

The problem is that:

import numpy as np
np = 1
np.log

is perfectly valid python.

>>> import numpy as np
>>> np = 1.
>>> np.log
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'float' object has no attribute 'log'

The solution is not to use np are a variable name, or other popular import abbreviations pd or dt etc. You can pick this kind of error up using a linter.

Share:
12,795
Tony
Author by

Tony

Updated on June 06, 2022

Comments

  • Tony
    Tony almost 2 years

    I have a time series with price information in column price. When I tried to create a new column ln_price by taking the ln of column price I got an error:

    AttributeError: 'float' object has no attribute 'log'

    Can someone help me understand why this would be and how it can be fixed?

    Thanks!

    df['ln_price'] = np.log(df['price'])
    
  • Warren Weckesser
    Warren Weckesser almost 5 years
    This is a possible source of the error, but not the only one. The error also occurs if df['price'].dtype is dtype('O') (i.e. the data type is object instead of float64). This is not unusual when working with Pandas.
  • Finomnis
    Finomnis almost 5 years
    @WarrenWeckesser Well, either way, we can 100% say that the problem is not part of the code you posted. Because your code works.
  • Warren Weckesser
    Warren Weckesser almost 5 years
    @Finomnis, I didn't post any code in this question. What code are you talking about?
  • Finomnis
    Finomnis almost 5 years
    df['ln_price'] = np.log(df['price'])
  • Warren Weckesser
    Warren Weckesser almost 5 years
    You assumed that df is a Python dictionary with a float value. I can create an example that reproduces the error when df is a Pandas DataFrame. See my answer to the question I linked to in a comment on the question.
  • Andy Hayden
    Andy Hayden almost 5 years
    @WarrenWeckesser AttributeError: 'float' object has no attribute 'log', the code you cide would not give that error.
  • Andy Hayden
    Andy Hayden almost 5 years
    @WarrenWeckesser "You assumed that df is a Python dictionary with a float value", no. That specific error can only happen at .log...
  • Warren Weckesser
    Warren Weckesser almost 5 years
    @AndyHayden: np.log(pd.Series(np.random.rand(10), dtype=object)) , where pd is pandas.
  • Warren Weckesser
    Warren Weckesser almost 5 years
    Even simpler: np.log(pd.Series([1.0, 'NA', 3.0]))
  • Andy Hayden
    Andy Hayden almost 5 years
    @WarrenWeckesser ooh, interesting. So it's to do with the dtype object, that's really weird.
  • Andy Hayden
    Andy Hayden almost 5 years
    @Finomnis looks like warren was correct: it's about the dtype! (I updated this answer to have some info), really weird.
  • Finomnis
    Finomnis almost 5 years
    Oh. What? That is super weird, but I think I don't know enough about pandas. I still think if OP used pandas, he should have mentioned it somewhere.
  • hpaulj
    hpaulj almost 5 years
    df is a common variable name for a pandas.DataFrame. Its indexing can look a lot like a dict's.