feat: new file
This commit is contained in:
21
backend/app/services/timeseries.py
Normal file
21
backend/app/services/timeseries.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import pandas as pd
|
||||
|
||||
|
||||
def normalize_timeseries(df: pd.DataFrame, ts_col: str, value_cols: list[str]) -> list[dict]:
|
||||
df = df.copy()
|
||||
df[ts_col] = pd.to_datetime(df[ts_col], errors="coerce")
|
||||
df = df.dropna(subset=[ts_col]).sort_values(ts_col)
|
||||
out = []
|
||||
for _, r in df.iterrows():
|
||||
p = {"ds": r[ts_col].to_pydatetime().isoformat()}
|
||||
for c in value_cols:
|
||||
v = r.get(c)
|
||||
try:
|
||||
p[c] = float(v) if v is not None else 0.0
|
||||
except Exception:
|
||||
p[c] = 0.0
|
||||
out.append(p)
|
||||
return out
|
||||
|
||||
Reference in New Issue
Block a user