Claude commited on
Commit
f074dd8
·
1 Parent(s): d707685

Fix TypeError in compute_summary when std() returns None

Browse files

mw.std() returns None (not a float) when the series has fewer than 2
non-null values (std is undefined for n=1). The existing len(mw) > 0
guard only catches an empty series, not the n=1 case, so float(None)
raised TypeError and the entire /api/data request returned 500.

Replace the four inline guards with a _safe() helper that checks the
scalar value itself for None before passing it to float().

https://claude.ai/code/session_01Vm5dTYdRf99LZ9MfB86vjx

Files changed (1) hide show
  1. app/services/data_processor.py +7 -4
app/services/data_processor.py CHANGED
@@ -186,12 +186,15 @@ def compute_summary(df: pl.DataFrame) -> dict:
186
  "pct": round(100 * count / total, 1) if total > 0 else 0,
187
  }
188
 
 
 
 
189
  return {
190
  "total_rows": total,
191
- "min_mw": round(float(mw.min()), 3) if len(mw) > 0 else None,
192
- "max_mw": round(float(mw.max()), 3) if len(mw) > 0 else None,
193
- "mean_mw": round(float(mw.mean()), 3) if len(mw) > 0 else None,
194
- "std_mw": round(float(mw.std()), 3) if len(mw) > 0 else None,
195
  "flag_breakdown": flag_breakdown,
196
  }
197
 
 
186
  "pct": round(100 * count / total, 1) if total > 0 else 0,
187
  }
188
 
189
+ def _safe(val) -> float | None:
190
+ return round(float(val), 3) if val is not None else None
191
+
192
  return {
193
  "total_rows": total,
194
+ "min_mw": _safe(mw.min() if len(mw) > 0 else None),
195
+ "max_mw": _safe(mw.max() if len(mw) > 0 else None),
196
+ "mean_mw": _safe(mw.mean() if len(mw) > 0 else None),
197
+ "std_mw": _safe(mw.std() if len(mw) > 0 else None),
198
  "flag_breakdown": flag_breakdown,
199
  }
200