1

I am calculating the distance between two points using the Harvesine formula available under mpu.haversine_distance. If I give a single pair of coordinate below code works. But, if I pass 5K cordinates, it gives me an exception.

import mpu
data = pd.read_csv(r'C:\Users\DATA.csv')
mpu.haversine_distance((40.717006,73.956565),(40.7205817,-73.9623624))
print (mpu.haversine_distance((data.pickup_lat,data.pickup_lon),(data.dropoff_lat,data.dropoff_lon)))

Exception:

    ~\AppData\Local\Programs\Python\Python38-32\lib\site-packages\mpu\__init__.py in haversine_distance(origin, destination)
    190     lat1, lon1 = origin
    191     lat2, lon2 = destination
--> 192     if not (-90.0 <= lat1 <= 90):
    193         raise ValueError('lat1={:2.2f}, but must be in [-90,+90]'.format(lat1))
    194     if not (-90.0 <= lat2 <= 90):

~\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\generic.py in __nonzero__(self)
   1550 
   1551     def __nonzero__(self):
-> 1552         raise ValueError(
   1553             "The truth value of a {0} is ambiguous. "
   1554             "Use a.empty, a.bool(), a.item(), a.any() or a.all().".format(

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

How to resolve this exception or ignore this this exception?

datapug
  • 2,261
  • 1
  • 17
  • 33
user2961127
  • 963
  • 2
  • 17
  • 29
  • Does this answer your question? [Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()](https://stackoverflow.com/questions/36921951/truth-value-of-a-series-is-ambiguous-use-a-empty-a-bool-a-item-a-any-o) – AMC Jan 12 '20 at 21:06
  • Haversine distance is the distance between two points on a sphere given their longitudes and latitudes. Can I ask you, what do you want to calculate passing to 5K points? Maybe you need to use lambda and some reduce logic – Vladimir Gurevich Jan 12 '20 at 21:11
  • Using 5K pair of points i want to calculate harvestine distance between each pair. – user2961127 Jan 12 '20 at 21:12
  • What does it mean? do you have a line? because without order(line) it doesn't make sense. if so, use lambda and pass each pair point and reduce – Vladimir Gurevich Jan 12 '20 at 21:14

1 Answers1

1

The syntax to apply a function to single values vs applying it in a dataframe is different. To get the distance between the points in case you are using a dataframe, you could use the option below (I replace the your data with a small example for testing purposes):

import pandas as pd
import mpu
import numpy as np

data = pd.DataFrame({"pickup_lat": [25.761681, 39.550053], "pickup_lon": [-80.191788, -80.191788],
                   "dropoff_lat": [25.790653, 36.169941], "dropoff_lon": [-80.130043, -115.139832]})
data["haversine_dist"] = data.apply(lambda x:
                                  mpu.haversine_distance((x["pickup_lat"], x["pickup_lon"]),
                                                         (x["dropoff_lat"], x["dropoff_lon"])), axis=1)
datapug
  • 2,261
  • 1
  • 17
  • 33