如果不满足任何条件,如何使用 np.select 引发值错误?
If no conditions are met, how do I raise a value error with np.select?
使用人工标记的数据,我有四个案例涵盖了正确的行为,但注释者可能会忽略提供值的需要。发生这种情况时,我希望程序引发错误。
#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"],
)
)
我愿意接受任何建议,我的主要问题是“如果没有满足任何情况,我该如何提出价值错误?”这样未来的团队成员就可以看到并解决错误。
您可以使用默认值,然后在np.select
之后检查默认值。如果有预定义的默认值,则引发错误。
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
使用人工标记的数据,我有四个案例涵盖了正确的行为,但注释者可能会忽略提供值的需要。发生这种情况时,我希望程序引发错误。
#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"],
)
)
我愿意接受任何建议,我的主要问题是“如果没有满足任何情况,我该如何提出价值错误?”这样未来的团队成员就可以看到并解决错误。
您可以使用默认值,然后在np.select
之后检查默认值。如果有预定义的默认值,则引发错误。
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