. Advertisement .
..3..
. Advertisement .
..4..
I am new to python and searching the “typeerror: cannot convert the series to ” to understand it better. It seems it doesn’t work as expected when I used some suggestions before. Here is the command line I use:
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)
The error I’m getting is below:
TypeError: cannot convert the series to <class 'float'>
Please give me the solution to this issue.
The cause: I think the cause of this error is that
Math.log
is looking for a single number, not an array of numbers.The solution: Instead, you can use
numpy.log
.To apply your functions to either the series or the data frame of pandas, 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.