Skip to content

Commit 0cac953

Browse files
Use explicit exceptions for rake validation
1 parent 519f7df commit 0cac953

2 files changed

Lines changed: 28 additions & 27 deletions

File tree

balance/weighting_methods/rake.py

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -206,30 +206,30 @@ def rake(
206206
result["weight"].tolist()
207207
# [1.0, 1.0]
208208
"""
209-
assert (
210-
"weight" not in sample_df.columns.values
211-
), "weight shouldn't be a name for covariate in the sample data"
212-
assert (
213-
"weight" not in target_df.columns.values
214-
), "weight shouldn't be a name for covariate in the target data"
209+
if "weight" in sample_df.columns.values:
210+
raise ValueError("weight shouldn't be a name for covariate in the sample data")
211+
if "weight" in target_df.columns.values:
212+
raise ValueError("weight shouldn't be a name for covariate in the target data")
215213

216214
# TODO: move the input checks into separate funnction for rake, ipw, poststratify
217-
assert isinstance(sample_df, pd.DataFrame), "sample_df must be a pandas DataFrame"
218-
assert isinstance(target_df, pd.DataFrame), "target_df must be a pandas DataFrame"
219-
assert isinstance(
220-
sample_weights, pd.Series
221-
), "sample_weights must be a pandas Series"
222-
assert isinstance(
223-
target_weights, pd.Series
224-
), "target_weights must be a pandas Series"
225-
assert sample_df.shape[0] == sample_weights.shape[0], (
226-
"sample_weights must be the same length as sample_df"
227-
f"{sample_df.shape[0]}, {sample_weights.shape[0]}"
228-
)
229-
assert target_df.shape[0] == target_weights.shape[0], (
230-
"target_weights must be the same length as target_df"
231-
f"{target_df.shape[0]}, {target_weights.shape[0]}"
232-
)
215+
if not isinstance(sample_df, pd.DataFrame):
216+
raise TypeError("sample_df must be a pandas DataFrame")
217+
if not isinstance(target_df, pd.DataFrame):
218+
raise TypeError("target_df must be a pandas DataFrame")
219+
if not isinstance(sample_weights, pd.Series):
220+
raise TypeError("sample_weights must be a pandas Series")
221+
if not isinstance(target_weights, pd.Series):
222+
raise TypeError("target_weights must be a pandas Series")
223+
if sample_df.shape[0] != sample_weights.shape[0]:
224+
raise ValueError(
225+
"sample_weights must be the same length as sample_df"
226+
f"{sample_df.shape[0]}, {sample_weights.shape[0]}"
227+
)
228+
if target_df.shape[0] != target_weights.shape[0]:
229+
raise ValueError(
230+
"target_weights must be the same length as target_df"
231+
f"{target_df.shape[0]}, {target_weights.shape[0]}"
232+
)
233233
if not isinstance(store_fit_metadata, bool):
234234
raise TypeError("`store_fit_metadata` must be a bool.")
235235
if store_fit_metadata and transformations == "default":
@@ -337,10 +337,11 @@ def rake(
337337
)
338338
if len(target_over_set):
339339
if len(alphabetized_variables) == 1:
340-
missing_mask = target_df[variable].isin(target_over_set)
341-
missing_level_target_weight = target_weights.loc[
342-
target_df.index[missing_mask]
343-
].sum()
340+
missing_mask = target_df[variable].isin(target_over_set).to_numpy()
341+
missing_indices = target_df.index[missing_mask]
342+
missing_level_target_weight = float(
343+
target_weights.loc[missing_indices].sum()
344+
)
344345
if missing_level_target_weight > 0:
345346
raise ValueError(
346347
"Single-variable rake requires that all target levels are "

tests/test_rake.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def _assert_rake_raises_with_message(
6767
**kwargs: Additional arguments to pass to rake()
6868
"""
6969
self.assertRaisesRegex(
70-
AssertionError,
70+
Exception,
7171
expected_message,
7272
rake,
7373
sample_df,

0 commit comments

Comments
 (0)