Формула Harversine для производства NaN с использованием pandas '0,23,4' - PullRequest
0 голосов
/ 26 февраля 2020

Привет! Я использовал формулу из этого поста. Быстрая аппроксимация Haversine (Python / Pandas) , и я не могу заставить ее работать над моим текущим набором данных.

Цель это добиться чего-то похожего на пост, который я связал выше. Я продолжаю сталкиваться со следующей ошибкой: main : 48: RuntimeWarning: недопустимое значение в arcsin

После создания столбца расстояния я хочу создать столбец скорости. Если у кого-то есть альтернативные решения, я также буду открыт для них.

Я отредактировал некоторые пути.

Пожалуйста, смотрите мой код ниже:

import pandas as pd
import os
import glob
import numpy as np

#Get directory of file

fpath = os.path.join('C:\\Users\\[USERNAME]','-------','Documents')
#Change directory to where files include
os.chdir(fpath)
filepath = fpath+"\\"+'*[latlong]*.csv'
#Import glob to follow pattern of files and read files in
files = [pd.read_csv(f) for f in glob.glob('C:\\Users\\[username\\folder\\Documents\\AMMlatlong*.csv')]
df = pd.concat(files)
#Format date types into date format
df.loc[:,'DateTime'] = pd.to_datetime(df.loc[:,'DateTime'], format='%b %d, %Y, %I:%M %p')
#Sort data by two columns datetime and customer id
df.sort_values(by=['Customer ID: Subscriber ID','DateTime'],ascending=True,inplace=True)
#Run calculation between previous row and next row for time between datetime
df['time_delta'] = (df['DateTime'] - df['DateTime'].shift()).astype('timedelta64[m]')
#Could also use the code to calculate column --> x['time_delta'] = x.timestamp.diff()

#Run haversine with row before and after

df['Lat'] , df['Long'] = df['Latitude Longitude'].str.split(',').str  
#Convert to float
df = df.apply(pd.to_numeric, errors='ignore') 
#Complete formula for calculating distance
from math import radians, cos, sin, asin, sqrt
def haversine(lat1, lon1, lat2, lon2, to_radians=True, earth_radius=6371):
    """
    slightly modified version: of http://stackoverflow.com/a/29546836/2901002

    Calculate the great circle distance between two points
    on the earth (specified in decimal degrees or in radians)

    All (lat, lon) coordinates must have numeric dtypes and be of equal length.

    """
    if to_radians:
        lat1, lon1, lat2, lon2 = map(radians,[lat1, lon1, lat2, lon2]) 

    dlon = lon2 - lon1
    dlat = lat2 - lat1

    a = np.sin((dlat)/2.0)**2 + \
        np.cos(lat1) * np.cos(lat2) * np.sin((dlon)/2.0)**2

    return earth_radius * 2 * np.arcsin(np.sqrt(a))

#Create columns that with a shifted value
df['p_lat'] = df.Lat.shift(1)
df['p_long'] = df.Long.shift(1)
#Columns with haversine formula
df['dist'] = haversine(df.p_lat, df.p_long, df.loc[1:,'Lat'], df.loc[1:,'Long']) 
...