Issue
I am trying to propagate uncertainty in Python by adding two columns together of a Pandas data frame and then taking the square root. However, when I try to use Python's math.sqrt
function, I receive a TypeError: cannot convert the series to <class 'float'>
.
Here is what I've been doing:
joinedDF['combined_error'] = math.sqrt((joinedDF.error1**2 + joinedDF.error2**2).astype(float))
Using joinedDF.dtypes
, I've verified that both error columns are float64
.
I've even tried seperating everything out, but I still receive a TypeError. Any suggestions about how to mend this?
Solution
Two options come to mind.
Option 1: use numpy.sqrt
:
import numpy as np
joinedDF['combined_error'] = np.sqrt((joinedDF['error1']**2 +
joinedDF['error2']**2))
Option 2: if you want/need to avoid numpy for some reason, you can apply
math.sqrt
to the numeric column. This is likely slower than Option 1, but works in my testing:
joinedDF['combined_error'] = (joinedDF['error1']**2 +
joinedDF['error2']**2).apply(math.sqrt)
Minor style comment: it's generally recommended to refer to DataFrame columns using indexing (square brackets) rather than attribute access (dot notation), so I modified your code accordingly. More reading: What is the difference between using squared brackets or dot to access a column?
Answered By - Peter Leimbigler
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.