Skip to content

Instantly share code, notes, and snippets.

@hansthen
Created October 26, 2023 07:19
Show Gist options
  • Save hansthen/b2508da7ca7f82593c653663de67f035 to your computer and use it in GitHub Desktop.
Save hansthen/b2508da7ca7f82593c653663de67f035 to your computer and use it in GitHub Desktop.
Compare two timeseries for distance
import pandas as pd
import numpy as np
import math
from datetime import datetime, timedelta
df1 = pd.DataFrame()
df1["time"] = pd.date_range('08/12/2021 00:00:01', periods=6, freq="4S")
df1['values'] = [1, 2, 3, 4, 5, 6]
df2 = pd.DataFrame()
df2["time"] = pd.date_range('08/12/2021', periods=4, freq="6S")
df2['values'] = [1, 2, 5, 200]
df = pd.merge(df1, df2, how="outer", left_on="time", right_on="time", sort=True)
print(df)
#df = df.interpolate(method="pad", axis=0)
df.ffill(inplace=True)
df.dropna(inplace=True)
#df.bfill(inplace=True)
df["time_to"] = df["time"].shift(-1)
df["time_to"].ffill(inplace=True)
df["duration"] = df["time_to"] - df["time"]
# Note: we do not have to use abs if d is a real distance
df["d"] = np.abs(np.arctan(df["values_x"] - df["values_y"]) / (math.pi / 2))
min_time = min(df["time"])
max_time = max(df["time_to"])
total = max_time - min_time
total = sum(df["duration"], timedelta())
print(total)
df["wd"] = df["d"] * ((df["time_to"] - df["time"]) / total)
print(df)
t2 = sum(df["wd"])
print(t2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment