Skip to content

Commit feabb54

Browse files
amuellerthomasjpfan
authored andcommitted
API Improves error msg when passing non-arrays to SimpleImputer (scikit-learn#14878)
1 parent 5c9b789 commit feabb54

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

sklearn/impute/_base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,9 @@ def _validate_input(self, X):
182182
force_all_finite=force_all_finite, copy=self.copy)
183183
except ValueError as ve:
184184
if "could not convert" in str(ve):
185-
raise ValueError("Cannot use {0} strategy with non-numeric "
186-
"data. Received datatype :{1}."
187-
"".format(self.strategy, X.dtype.kind))
185+
new_ve = ValueError("Cannot use {} strategy with non-numeric "
186+
"data:\n{}".format(self.strategy, ve))
187+
raise new_ve from None
188188
else:
189189
raise ve
190190

sklearn/impute/tests/test_impute.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,23 @@ def test_imputation_mean_median_error_invalid_type(strategy, dtype):
237237
X = np.array([["a", "b", 3],
238238
[4, "e", 6],
239239
["g", "h", 9]], dtype=dtype)
240+
msg = "non-numeric data:\ncould not convert string to float: '"
241+
with pytest.raises(ValueError, match=msg):
242+
imputer = SimpleImputer(strategy=strategy)
243+
imputer.fit_transform(X)
240244

241-
with pytest.raises(ValueError, match="non-numeric data"):
245+
246+
@pytest.mark.parametrize("strategy", ["mean", "median"])
247+
@pytest.mark.parametrize("type", ['list', 'dataframe'])
248+
def test_imputation_mean_median_error_invalid_type_list_pandas(strategy, type):
249+
X = [["a", "b", 3],
250+
[4, "e", 6],
251+
["g", "h", 9]]
252+
if type == 'dataframe':
253+
pd = pytest.importorskip("pandas")
254+
X = pd.DataFrame(X)
255+
msg = "non-numeric data:\ncould not convert string to float: '"
256+
with pytest.raises(ValueError, match=msg):
242257
imputer = SimpleImputer(strategy=strategy)
243258
imputer.fit_transform(X)
244259

0 commit comments

Comments
 (0)