From 804a4a096669706b1da0da917d6c91700ada07f1 Mon Sep 17 00:00:00 2001 From: Alexander Hess Date: Tue, 1 Dec 2026 14:49:03 +0100 Subject: [PATCH] Iterate over the rows in `Forecast.from_dataframe()` --- src/velofy/db/forecasts.py | 46 ++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 27 deletions(-) diff --git a/src/velofy/db/forecasts.py b/src/velofy/db/forecasts.py index 0f04a45..a7246ef 100644 --- a/src/velofy/db/forecasts.py +++ b/src/velofy/db/forecasts.py @@ -222,40 +222,32 @@ class Forecast(meta.Base): """ forecasts = [] - for timestamp_idx in data.index: - start_at = timestamp_idx.to_pydatetime() - actual = int(data.loc[timestamp_idx, "actual"]) - prediction = float(round(data.loc[timestamp_idx, "prediction"], 5)) - - # Explicit type casting. SQLAlchemy does not convert - # `float('NaN')`s into plain `None`s. - - low80 = data.loc[timestamp_idx, "low80"] - high80 = data.loc[timestamp_idx, "high80"] - low95 = data.loc[timestamp_idx, "low95"] - high95 = data.loc[timestamp_idx, "high95"] - - low80 = None if math.isnan(low80) else float(round(low80, 5)) - - high80 = None if math.isnan(high80) else float(round(high80, 5)) - - low95 = None if math.isnan(low95) else float(round(low95, 5)) - - high95 = None if math.isnan(high95) else float(round(high95, 5)) + rows = data[["actual", "prediction", "low80", "high80", "low95", "high95"]] + for ( + timestamp_idx, + actual, + prediction, + low80, + high80, + low95, + high95, + ) in rows.itertuples(name=None): forecasts.append( cls( pixel=pixel, - start_at=start_at, + start_at=timestamp_idx.to_pydatetime(), time_step=time_step, train_horizon=train_horizon, model=model, - actual=actual, - prediction=prediction, - low80=low80, - high80=high80, - low95=low95, - high95=high95, + actual=int(actual), + prediction=float(round(prediction, 5)), + # Explicit type casting. SQLAlchemy does not convert + # `float('NaN')`s into plain `None`s. + low80=None if math.isnan(low80) else float(round(low80, 5)), + high80=None if math.isnan(high80) else float(round(high80, 5)), + low95=None if math.isnan(low95) else float(round(low95, 5)), + high95=None if math.isnan(high95) else float(round(high95, 5)), ), )