Skip to content

Commit e0d7c00

Browse files
committed
CategoricalImputer: Error out when no mode is found
1 parent c2bccd1 commit e0d7c00

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
@@ -65,7 +65,12 @@ def fit(self, X, y=None):
6565
mask = _get_mask(X, self.missing_values)
6666
X = X[~mask]
6767

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

7075
return self
7176

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)