Home > OS >  If no conditions are met, how do I raise a value error with np.select?
If no conditions are met, how do I raise a value error with np.select?

Time:05-17

Working with human labeled data, I have four cases that cover correct behavior, but it is possible for annotators to miss the need to provide a value. When this happens, I want the program to raise an error.

    #selects the final variation based on presence of other accepted variations
        dataframe[audit_cols["final_variation"]] = np.select(
            #conditions
            [
                dataframe[audit_cols["validation_step_new_variation"]] != None,
                dataframe[audit_cols["annotation_step_keyword"]] != None, 
                dataframe[audit_cols["keyword_feasible"]] == "NO", 
            ],
            #actions based on conditions
            [
                dataframe["validation_step_new_variation"]],
                dataframe["annotation_step_keyword"], 
                #leaves blank if no variation seems feasible
                "",
            ],
    
            #raises error if no conditions are met
            default = raise ValueError(
                    "No variation selected and the keyword has not been marked as unfeasible for row UID:",
                    dataframe["unique_ID"],
                )
    
        ) 

I'm open to any suggestions, and my primary question is "how do I raise a value error if no cases are met?" This is so future team members can see and address the eror.

CodePudding user response:

You can use a default value then check the default value after np.select. If there is predefined default value, raise the error.

default_value = 'no condition met'

#selects the final variation based on presence of other accepted variations
dataframe[audit_cols["final_variation"]] = np.select(
    # ...
    default = default_value
)

error_flag = dataframe[audit_cols["final_variation"]].eq(default_value).sum().sum() > 1

if error_flag:
   raise ValueError
  • Related