Skip to content

Commit 3d30bc5

Browse files
igerberclaude
andcommitted
Add non-finite outcome validation in EfficientDiD.fit()
Reject NaN/Inf values in the outcome column before they can propagate into Omega*/EIF calculations. Adds a regression test for the validation. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent cda0153 commit 3d30bc5

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

diff_diff/efficient_did.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,15 @@ def fit(
217217
"panel where every unit is observed in every time period."
218218
)
219219

220+
# Reject non-finite outcomes (NaN/Inf corrupt Omega*/EIF calculations)
221+
non_finite_mask = ~np.isfinite(df[outcome])
222+
if non_finite_mask.any():
223+
n_bad = int(non_finite_mask.sum())
224+
raise ValueError(
225+
f"Found {n_bad} non-finite value(s) in outcome column '{outcome}'. "
226+
"EfficientDiD requires finite outcomes for all unit-period observations."
227+
)
228+
220229
# Reject duplicate (unit, time) rows
221230
dup_mask = df.duplicated(subset=[unit, time], keep=False)
222231
if dup_mask.any():

tests/test_efficient_did.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,13 @@ def test_pt_post_no_never_treated_raises(self):
321321
with pytest.raises(ValueError, match="never-treated"):
322322
EfficientDiD(pt_assumption="post").fit(df, "y", "unit", "time", "first_treat")
323323

324+
def test_nan_outcome_raises(self):
325+
"""Non-finite outcomes in a balanced panel should be rejected."""
326+
df = _make_simple_panel()
327+
df.loc[df.index[0], "y"] = np.nan
328+
with pytest.raises(ValueError, match="non-finite"):
329+
EfficientDiD().fit(df, "y", "unit", "time", "first_treat")
330+
324331
def test_duplicate_unit_time_raises(self):
325332
"""Duplicate (unit, time) rows should be rejected."""
326333
df = _make_simple_panel()

0 commit comments

Comments
 (0)