I am trying to run a calculation on a Pandas Dataframe and don’t understand why the direct calculation fails and the loop calculation works. The direct calculation works if I remove the math.sqrt function.
a
, b
and c
are float constants.
This line fails:
grid['Conf'] = (-b + math.sqrt(b*b-4.0*a*(c-grid['Distance'])))/(2.0*a)
The error is
--> 185 raise TypeError(f"cannot convert the series to {converter}")
TypeError: cannot convert the series to <class 'float'>
But this works (incorrect results since the sqrt is removed):
grid['Conf'] = (-b + (b*b-4.0*a*(c-grid['Distance'])))/(2.0*a)
And this works (correct result but takes a long time to calculate):
for i in grid.index:
grid['Conf'].iloc[i] = (-b + math.sqrt(b*b-4.0*a*(c-grid['Distance'].iloc[i])))/(2.0*a)
New contributor
chadn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.