'Float' object has no attribute 'log'
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.
Tony
Updated on June 06, 2022Comments
-
Tony almost 2 years
I have a time series with price information in column
price
. When I tried to create a new columnln_price
by taking theln
of columnprice
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 almost 5 yearsThis is a possible source of the error, but not the only one. The error also occurs if
df['price'].dtype
isdtype('O')
(i.e. the data type isobject
instead offloat64
). This is not unusual when working with Pandas. -
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 almost 5 years@Finomnis, I didn't post any code in this question. What code are you talking about?
-
Finomnis almost 5 years
df['ln_price'] = np.log(df['price'])
-
Warren Weckesser almost 5 yearsYou assumed that
df
is a Python dictionary with afloat
value. I can create an example that reproduces the error whendf
is a Pandas DataFrame. See my answer to the question I linked to in a comment on the question. -
Andy Hayden almost 5 years@WarrenWeckesser
AttributeError: 'float' object has no attribute 'log'
, the code you cide would not give that error. -
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 almost 5 years@AndyHayden:
np.log(pd.Series(np.random.rand(10), dtype=object))
, wherepd
ispandas
. -
Warren Weckesser almost 5 yearsEven simpler:
np.log(pd.Series([1.0, 'NA', 3.0]))
-
Andy Hayden almost 5 years@WarrenWeckesser ooh, interesting. So it's to do with the dtype object, that's really weird.
-
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 almost 5 yearsOh. 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 almost 5 years
df
is a common variable name for apandas.DataFrame
. Its indexing can look a lot like a dict's.