Skip to content
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

Remove unnecessary OneHotEncoder when there are nan values #725

Merged
merged 5 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion rdt/transformers/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ def _transform(self, data):
"""
data = self._prepare_data(data)
unique_data = {np.nan if pd.isna(x) else x for x in pd.unique(data)}
unseen_categories = unique_data - set(self.dummies)
unseen_categories = unique_data - {np.nan if pd.isna(x) else x for x in self.dummies}
if unseen_categories:
# Select only the first 5 unseen categories to avoid flooding the console.
examples_unseen_categories = set(list(unseen_categories)[:5])
Expand Down
23 changes: 23 additions & 0 deletions tests/integration/transformers/test_categorical.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import pickle
import tempfile
import warnings
from io import BytesIO

import numpy as np
Expand Down Expand Up @@ -424,6 +426,26 @@ def test_one_hot_numerical_nans():
pd.testing.assert_frame_equal(reverse, data)


def test_one_hot_doesnt_warn():
"""Ensure OneHotEncoder doesn't warn when saving and loading GH#616."""
# Setup
data = pd.DataFrame({'column_name': [1.0, 2.0, np.nan, 2.0, 3.0, np.nan, 3.0]})
ohe = OneHotEncoder()

# Run
ohe.fit(data, 'column_name')
# Create a temporary file and dump the transformer
tmp = tempfile.NamedTemporaryFile()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use the pytest tmp_path fixutre instead?

pickle.dump(ohe, tmp)
tmp.seek(0)
ohe_loaded = pickle.load(tmp)

# Assert
with warnings.catch_warnings():
warnings.simplefilter('error')
ohe_loaded.transform(data)


def test_label_numerical_2d_array():
"""Ensure LabelEncoder works on numerical + nan only columns."""

Expand All @@ -432,6 +454,7 @@ def test_label_numerical_2d_array():

transformer = LabelEncoder()
transformer.fit(data, column)

transformed = pd.DataFrame([0., 1., 2., 3.], columns=['column_name'])
reverse = transformer.reverse_transform(transformed)

Expand Down
Loading