Skip to content

edit _transform_count_encode #261

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

Merged
merged 2 commits into from
Oct 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion category_encoders/count.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,11 @@ def _fit_count_encode(self, X_in, y):
def _transform_count_encode(self, X_in, y):
"""Perform the transform count encoding."""
X = X_in.copy(deep=True)
X.fillna(value=np.nan, inplace=True)

for col in self.cols:

X[col] = X.fillna(value=np.nan)[col]

if self._min_group_size is not None:
if col in self._min_group_categories.keys():
X[col] = (
Expand Down
12 changes: 12 additions & 0 deletions tests/test_encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,3 +692,15 @@ def test_columns(self):
self.assertTrue(result['float_edge'].min() < 1, 'should still be a number and untouched')
self.assertTrue(result['unique_int'].min() < 1, 'should still be a number and untouched')
self.assertTrue(result[321].min() < 1, 'should still be a number')

def test_ignored_columns_are_untouched(self):
# Make sure None values in ignored columns are preserved.
# See: https://github.com/scikit-learn-contrib/category_encoders/pull/261
X = pd.DataFrame({'col1': ['A', 'B', None], 'col2': ['C', 'D', None]})
y = [1, 0, 1]

for encoder_name in (set(encoders.__all__)):
with self.subTest(encoder_name=encoder_name):
enc = getattr(encoders, encoder_name)(cols=['col1'])
out = enc.fit_transform(X, y)
self.assertTrue(out.col2[2] is None)