Skip to content

Commit ef5d543

Browse files
svenchiltonclaude
andcommitted
fix(examples): guard against ZeroDivisionError in PII subset stats
Address CodeRabbit feedback on PR #1906 (examples/notebooks/data/build_pii_detection_subset.py around line 293). The "Avg entities per PII-bearing row" stat computed `entity_count / (len(rows) - rows_no_pii)`, which would raise ZeroDivisionError if a user reused the script to build a subset where every row is an FP-test row (no PII). The current 20-row subset has 17 PII / 3 no-PII rows so this never trips in practice, but the build script is meant to be reusable. Fix: extract `rows_with_pii = len(rows) - rows_no_pii`, guard with `if rows_with_pii:`, and print "N/A (no PII-bearing rows in subset)" in the fallback case. Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 4f4d7fe commit ef5d543

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

examples/notebooks/data/build_pii_detection_subset.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,13 @@ def main() -> None:
288288
# Quick stats
289289
entity_count = sum(len(json.loads(r["entities"])) for r in rows)
290290
rows_no_pii = sum(1 for r in rows if json.loads(r["entities"]) == [])
291+
rows_with_pii = len(rows) - rows_no_pii
291292
print(f"Total entities: {entity_count}")
292293
print(f"Rows with no PII (FP-test rows): {rows_no_pii}")
293-
print(f"Avg entities per PII-bearing row: {entity_count / (len(rows) - rows_no_pii):.1f}")
294+
if rows_with_pii:
295+
print(f"Avg entities per PII-bearing row: {entity_count / rows_with_pii:.1f}")
296+
else:
297+
print("Avg entities per PII-bearing row: N/A (no PII-bearing rows in subset)")
294298

295299

296300
if __name__ == "__main__":

0 commit comments

Comments
 (0)