Skip to content

Commit 5eb6c50

Browse files
committed
CategoricalImputer: Error out when no mode is found
1 parent 261e7e5 commit 5eb6c50

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

sklearn_pandas/categorical_imputer.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,12 @@ def fit(self, X, y=None):
6464
mask = _get_mask(X, self.missing_values)
6565
X = X[~mask]
6666

67-
self.fill_ = Counter(X).most_common(1)[0][0]
67+
modes = pd.Series(X).mode()
68+
if modes.shape[0] == 0:
69+
raise ValueError('No value is repeteated more than '
70+
'twice in the column')
71+
else:
72+
self.fill_ = modes[0]
6873

6974
return self
7075

tests/test_categorical_imputer.py

+14
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@ def test_unit(input_type, none_value):
3434
assert (Xt == ['a', 'b', 'b', 'b']).all()
3535

3636

37+
@pytest.mark.parametrize('input_type', ['np', 'pd'])
38+
def test_no_mode(input_type):
39+
40+
data = ['a', 'b', 'c', np.nan]
41+
42+
if input_type == 'pd':
43+
X = pd.Series(data)
44+
else:
45+
X = np.asarray(data, dtype=object)
46+
47+
with pytest.raises(ValueError):
48+
CategoricalImputer().fit_transform(X)
49+
50+
3751
@pytest.mark.parametrize('input_type', ['np', 'pd'])
3852
def test_missing_values_param(input_type):
3953

0 commit comments

Comments
 (0)