Skip to content

Instantly share code, notes, and snippets.

@saswata-dutta
Created October 29, 2019 10:29
Show Gist options
  • Save saswata-dutta/45def13d29a5dc34f3f3fe1dfd06844b to your computer and use it in GitHub Desktop.
Save saswata-dutta/45def13d29a5dc34f3f3fe1dfd06844b to your computer and use it in GitHub Desktop.
cumulative haversion on lat lon of previous row
from datetime import datetime
import time
def as_epoch(row):
dt = datetime.strptime(row['gps_timestamp'], '%Y-%m-%d %H:%M:%S%z')
return int(time.mktime(dt.timetuple()))
df['epoch'] = df.apply (lambda row: as_epoch(row), axis=1)
df = df.drop('gps_timestamp', axis=1)
df = df.sort_values('epoch')
from haversine import haversine, Unit
def haversine_dist(row):
index = df.index.get_loc(row.name)
if index == 0:
return 0
prev_row = df.iloc[index - 1]
return haversine((row['latitude'], row['longitude']), (prev_row['latitude'], prev_row['longitude']))
df['haversine'] = df.apply (haversine_dist, axis=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment