Skip to content

Commit 519f7df

Browse files
Fix single-variable rake validation
1 parent 54fd03e commit 519f7df

2 files changed

Lines changed: 13 additions & 13 deletions

File tree

balance/weighting_methods/rake.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,11 @@ def rake(
267267
(sample_df, target_df)
268268
)
269269

270-
assert len(variables) > 0, (
271-
"Must weight on at least one variable. "
272-
"Received no common variables between sample and target."
273-
)
270+
if len(variables) == 0:
271+
raise ValueError(
272+
"Must weight on at least one variable. "
273+
"Received no common variables between sample and target."
274+
)
274275

275276
if store_fit_metadata:
276277
# Fail fast: persisting non-pickleable callables (e.g. lambdas,
@@ -317,9 +318,6 @@ def rake(
317318
f"Alphabetized variable order is as follows: {alphabetized_variables}."
318319
)
319320

320-
target_df = target_df.assign(weight=target_weights)
321-
sample_df = sample_df.assign(weight=sample_weights)
322-
323321
# Cast all data types as string to be explicit about each unique value
324322
# being its own group and to handle that `fillna()` above creates
325323
# series of type Object, which won't work for the ipfn script
@@ -339,8 +337,9 @@ def rake(
339337
)
340338
if len(target_over_set):
341339
if len(alphabetized_variables) == 1:
342-
missing_level_target_weight = target_df.loc[
343-
target_df[variable].isin(target_over_set), "weight"
340+
missing_mask = target_df[variable].isin(target_over_set)
341+
missing_level_target_weight = target_weights.loc[
342+
target_df.index[missing_mask]
344343
].sum()
345344
if missing_level_target_weight > 0:
346345
raise ValueError(
@@ -359,6 +358,9 @@ def rake(
359358
f"Final covariates and levels that will be used in raking: {dict(zip(alphabetized_variables, categories))}."
360359
)
361360

361+
target_df = target_df.assign(weight=target_weights)
362+
sample_df = sample_df.assign(weight=sample_weights)
363+
362364
sample_sum_weights = sample_df["weight"].sum()
363365
target_sum_weights = target_df["weight"].sum()
364366

tests/test_rake.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,7 @@ def test_rake_input_assertions(self) -> None:
134134
)
135135
self.assertIn("weight", single_var_result)
136136

137-
with self.assertRaisesRegex(
138-
AssertionError, "Must weight on at least one variable"
139-
):
137+
with self.assertRaisesRegex(ValueError, "Must weight on at least one variable"):
140138
rake(
141139
sample_df=pd.DataFrame({"a": [1, 2]}),
142140
sample_weights=pd.Series([1.0, 1.0]),
@@ -339,7 +337,7 @@ def test_rake_single_variable_default_transform_with_na_predict_works(self) -> N
339337
atol=1e-8,
340338
)
341339

342-
def test_rake_single_variable_target_only_levels_rejected_in_transfer(self) -> None:
340+
def test_rake_single_variable_target_only_levels_rejected_at_fit(self) -> None:
343341
sample_df = pd.DataFrame({"x": ["a", "a", "b", "b"]})
344342
target_df = pd.DataFrame({"x": ["a", "a", "b", "b", "c"]})
345343
sample_w = pd.Series([1.0, 1.0, 1.0, 1.0])

0 commit comments

Comments
 (0)