Created
October 29, 2019 10:29
-
-
Save saswata-dutta/45def13d29a5dc34f3f3fe1dfd06844b to your computer and use it in GitHub Desktop.
cumulative haversion on lat lon of previous row
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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