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
Changes from 1 commit
b895f7c
34fb9a3
3abc695
05226fd
c122315
4915a7e
d266909
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.""" | ||
if (X.dtype.kind in ("f", "i", "u") and | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 {}." | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
@@ -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 | ||
|
@@ -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" | ||
|
@@ -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: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
There was a problem hiding this comment.
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?