Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MRG] FIX: raise error with inconsistent dtype X and missing_values #11391

Merged
merged 7 commits into from Jul 16, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion sklearn/impute.py
Expand Up @@ -40,6 +40,17 @@
]


def _check_inputs_dtype(X, missing_values):
"""Check that the dtype of X is in accordance with the one of
missing_values."""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would phrase this in the other order
"""
Check that the type of missing_values is in accordance with the dtype of X.
"""
as that's what's we are checking right? I mean when we get this error, the user should change the value of missing_value not the other way around, right?

if (X.dtype.kind in ("f", "i", "u") and
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice! I didn't know about this.

not isinstance(missing_values, numbers.Real)):
raise TypeError("The data type of 'missing_values' and 'X' are "
"not compatible. 'missing_values' data type is "
"{} and 'X' is {}."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the rest of the code, we raise a ValueError for this kind of error, there is a discussion in #11211.

.format(type(missing_values), X.dtype))


def _get_mask(X, value_to_mask):
"""Compute the boolean mask X == missing_values."""
if value_to_mask is np.nan:
Expand All @@ -51,7 +62,6 @@ def _get_mask(X, value_to_mask):
else:
# np.isnan does not work on object dtypes.
return _object_dtype_isnan(X)

else:
# X == value_to_mask with object dytpes does not always perform
# element-wise for old versions of numpy
Expand Down Expand Up @@ -183,6 +193,7 @@ def _validate_input(self, X):
else:
raise ve

_check_inputs_dtype(X, self.missing_values)
if X.dtype.kind not in ("i", "u", "f", "O"):
raise ValueError("SimpleImputer does not support data with dtype "
"{0}. Please provide either a numeric array (with"
Expand Down Expand Up @@ -788,6 +799,7 @@ def _initial_imputation(self, X):

X = check_array(X, dtype=FLOAT_DTYPES, order="F",
force_all_finite=force_all_finite)
_check_inputs_dtype(X, self.missing_values)

mask_missing_values = _get_mask(X, self.missing_values)
if self.initial_imputer_ is None:
Expand Down
20 changes: 20 additions & 0 deletions sklearn/tests/test_impute.py
Expand Up @@ -705,3 +705,23 @@ def test_chained_imputer_additive_matrix():
random_state=rng).fit(X_train)
X_test_est = imputer.transform(X_test)
assert_allclose(X_test_filled, X_test_est, atol=0.01)


@pytest.mark.parametrize("imputer_constructor",
[SimpleImputer, ChainedImputer])
@pytest.mark.parametrize(
"missing_values, X_missing_value, err_type, err_msg",
[("NaN", np.nan, ValueError, "contains"),
("-1", -1, TypeError, "not compatible")])
def test_inconsistent_dtype_X_missing_values(imputer_constructor,
missing_values, X_missing_value,
err_type, err_msg):
# regression test for issue #11390. Comparison between incoherent dtype
# for X and missing_values was not raising a proper error.
X = np.random.randn(1000, 10)
X[0, 0] = X_missing_value

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure 1000 is necessary here :)

imputer = imputer_constructor(missing_values=missing_values)

with pytest.raises(err_type, match=err_msg):
imputer.fit_transform(X)