Python Forum
Formula works for one row does not for two
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Formula works for one row does not for two
#1
Hi

Trying to extract the core of the problem, I have found that a formula works for one row only, if another row is added, an error shows:
cannot convert the series to <class 'float'>

By the way, does not work even if the content is turned to float beforehand.

So here it is

import pandas as pd
import numpy as np

startlat=51.454234 
startlon=-2.593225

df6 = pd.DataFrame(columns=['Latitude', 'Longitude'])
df6.loc[0] = [51.3862652,  -2.3638941]
# df6.loc[1] = [51.3862652,  -2.3638941]

df6['dist1']=6371.01 * np.arccos(np.sin(np.radians(float(startlat)))*np.sin(np.radians(float(df6['Latitude']))) + np.cos(np.radians(float(startlat)))*np.cos(np.radians(float(df6['Latitude'])))*np.cos(np.radians(float(startlon)) - np.radians(float(df6['Longitude']))))
df6.head()
This works. However, when I uncomment the addition of the second row and run this all again, python returns an error: TypeError: cannot convert the series to <class 'float'>

What is the cause and how to work around that?

Thank you.
Reply
#2
PS.
I am using Collab. The formula in use is proven to work correctly. I have not found any library to deal with that more elegantly.


Here are the output of the first go and the full message:

Output:
Latitude Longitude dist9 0 51.386265 -2.363894 17.606796
Error:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-309-573fe0c6d115> in <module>() 12 13 df6.reset_index ---> 14 df6['dist2']=6371.01 * np.arccos(np.sin(np.radians(float(startlat)))*np.sin(np.radians(float(df6['Latitude']))) + np.cos(np.radians(float(startlat)))*np.cos(np.radians(float(df6['Latitude'])))*np.cos(np.radians(float(startlon)) - np.radians(float(df6['Longitude'])))) 15 df6.head() /usr/local/lib/python3.6/dist-packages/pandas/core/series.py in wrapper(self) 91 return converter(self.iloc[0]) 92 raise TypeError("cannot convert the series to " ---> 93 "{0}".format(str(converter))) 94 95 wrapper.__name__ = "__{name}__".format(name=converter.__name__) TypeError: cannot convert the series to <class 'float'>
Thank you for your help.
Reply
#3
Have a look at this code:
import pandas as pd
import numpy as np

start_lat = 51.454234 
start_lon = -2.593225

start_lat = np.radians(start_lat)
start_lon = np.radians(start_lon)

data = {'latitudes': [51.3862652, 51.4862652, 51.5862652, 51.6862652, 51.7862652, 52.406374],
        'longitudes': [-2.3638941, -2.693225, -2.793225, -2.893225, -2.993225, 16.9251681]}
 
df = pd.DataFrame(data)

df['latitudes'] = np.radians(df['latitudes'])
df['longitudes'] = np.radians(df['longitudes'])

R = 6371.01

df['distance'] = R * np.arccos(np.sin(start_lat) * np.sin(df['latitudes']) + np.cos(start_lat) * np.cos(df['latitudes']) * np.cos(start_lon - df['longitudes']))
df
Reply
#4
Thanks a lot. So, in essence, numpy does not like making the calculation of radians part of the equasion when there are more rows than 1.

Would you please be able to explain what makes the difference, and what made you think this is the answer?
At first, I thought it had to do with the type of table but this is I think already ruled out by the fact that removing the second row in my example makes it possible to apply the complete formula again.
Reply
#5
(Aug-25-2019, 08:57 PM)Prometheus Wrote: So, in essence, numpy does not like making the calculation of radians part of the equasion when there are more rows than 1.

No, the problem were all these float(df6['Latitude']) etc
to change the data type a column (series) in a dataframe you need to call the appropriate pandas methods to do so.
You can put the np.radians() back into the calculation and it will work also.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020