@@ -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
0 commit comments