diff --git a/src/qc_qa/confidence/summary.py b/src/qc_qa/confidence/summary.py index 72d1f7f..2c65d44 100644 --- a/src/qc_qa/confidence/summary.py +++ b/src/qc_qa/confidence/summary.py @@ -140,14 +140,22 @@ def compute_flagged( below_mask = series < threshold if not below_mask.any(): continue - below = final_df.loc[below_mask, [c for c in (fn_col, field) if c]] + # Only select columns that actually exist. A _CONF column can + # survive into final_df without its sibling value column -- + # e.g. an internal-only confidence-bearing column whose value side + # was dropped by reorder_columns. Without this guard, .loc throws + # KeyError: "[''] not in index" and crashes the run. + select_cols = [c for c in (fn_col, field) if c and c in final_df.columns] + below = final_df.loc[below_mask, select_cols] for idx, row in below.iterrows(): flagged.append( { - "FILE_NAME": row[fn_col] if fn_col else "", + "FILE_NAME": ( + row[fn_col] if fn_col and fn_col in below.columns else "" + ), "field": field, "confidence": round(float(series.loc[idx]), 4), - "value": "" if field not in below.columns else row.get(field, ""), + "value": row.get(field, "") if field in below.columns else "", "threshold": threshold, } )