. Advertisement .
..3..
. Advertisement .
..4..
I’m trying to run a new project. I do a couple of things like this:
date A
2001-01-02 1.0022
2001-01-03 1.1033
2001-01-04 1.1496
2001-01-05 1.1033
2015-03-30 126.3700
2015-03-31 124.4300
2015-04-01 124.2500
2015-04-02 124.8900
df["B"] = math.log(df["A"] / df["A"].shift(1))
df["B"] .astype(float)
But in my program, I am getting the warning:
TypeError: cannot convert the series to <class 'float'>
Can someone explain why the “cannot convert the series to” issue happened? Where have I gone wrong? Thank you!
The cause:
This error occurs due to you just write
df["A"].astype(float)
, it can not changedf
.Solution:
You can resolve this error by assigning the output of the astype method call to another method, consist of the existing series by using
df['A'] = df['A'].astype(float)
. The other way to solve this issue is that you can either use numpy as @user3582076 suggestions or.apply
on the Series that is a result of dividing today’s value by yesterday’s.To apply your functions to either the pandas series or data frame, you can use the lambda operator. You can convert every element in a column to an floating point number by using the lambda operator.
Here, the lambda operator will take values from that column (as “x”) and return them as a floating value.