Skip to content

Commit

Permalink
split errors in two
Browse files Browse the repository at this point in the history
  • Loading branch information
Tamar Grey committed Oct 3, 2022
1 parent 43dc422 commit f791cfd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,16 @@ def fit(self, X, y=None):
elif self.parameters["categories"] is not None:
# Categories specified - make sure they match the ordinal columns
input_categories = self.parameters["categories"]
if len(input_categories) != len(self.features_to_encode) or not isinstance(
input_categories[0],
list,
):

if len(input_categories) != len(self.features_to_encode):
raise ValueError(
"Categories argument must contain a list of categories for each ordinal feature",
"Categories argument must contain as many elements as there are Ordinal features.",
)

if not all(isinstance(cats, list) for cats in input_categories):
raise ValueError(
"Each element of the categories argument must be a list.",
)
# Categories, as they're passed into SKOrdinalEncoder should be in the same order
# as the data's Ordinal.order categories even if it's a subset
for i, col_categories in enumerate(input_categories):
Expand Down Expand Up @@ -210,7 +212,7 @@ def transform(self, X, y=None):
Returns:
pd.DataFrame: Transformed data, where each ordinal feature has been encoded into
a numerical column using where of ordinal integers represent
a numerical column where ordinal integers represent
the relative order of categories.
"""
X = infer_feature_types(X)
Expand Down
11 changes: 3 additions & 8 deletions evalml/tests/component_tests/test_ordinal_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from evalml.exceptions import ComponentNotYetFittedError
from evalml.pipelines.components import OrdinalEncoder
from evalml.utils import get_random_seed


def set_first_three_columns_to_ordinal_with_categories(X, categories):
Expand Down Expand Up @@ -68,16 +67,12 @@ def test_invalid_inputs():
[["a", "b", "c", "d"], ["a", "b", "c"], ["a"]],
)
encoder = OrdinalEncoder(categories=[["a", "b"], ["a", "c"]])
error_msg = (
"Categories argument must contain a list of categories for each ordinal feature"
)
error_msg = "Categories argument must contain as many elements as there are Ordinal features."
with pytest.raises(ValueError, match=error_msg):
encoder.fit(X)

encoder = OrdinalEncoder(categories=["a", "b", "c"])
error_msg = (
"Categories argument must contain a list of categories for each ordinal feature"
)
encoder = OrdinalEncoder(categories=[["a", "b"], 1, ["a"]])
error_msg = "Each element of the categories argument must be a list."
with pytest.raises(ValueError, match=error_msg):
encoder.fit(X)

Expand Down

0 comments on commit f791cfd

Please sign in to comment.