I'm trying to calculate speed between consecutive timepoints given data that is in a '.csv' file with the following columns: "Time Elapsed", "x", and "y". The ultimate goal is to get the data into a format where I can plot "Time Elapsed" vs. "speed"
I'm fairly sure that my implementation is doing what I want, but it's certainly possible (and likely) that I overlooked something. I'm also wondering whether there are faster/more efficient ways (in Python) to perform these calculations?
import pandas as pd
import numpy as np
def calculate_speeds(path_to_csv):
data_df = pd.read_csv(path_to_csv)
xy = data_df[['x', 'y']]
b = np.roll(xy, -1, axis=0)[:-1]
a = xy[:-1]
dxy = np.linalg.norm(a - b, axis=1)
dt = (np.roll(data_df['Time Elapsed'], -1) - data_df['Time Elapsed'])[:-1]
speeds = np.divide(dxy, dt)
speed_df = pd.DataFrame(data={'Time Elapsed':data_df['Time Elapsed'][:-1],'Speed':speeds})
return speed_df