Skip to content
This repository has been archived by the owner on Jan 9, 2024. It is now read-only.

Commit

Permalink
Merge pull request #19 from georgianpartners/issue_17
Browse files Browse the repository at this point in the history
Change np.isnan to pd.isnull to get around string error, issue #17
  • Loading branch information
jichaogp committed Nov 26, 2018
2 parents 5cc9025 + 31b764d commit 4254249
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
13 changes: 13 additions & 0 deletions foreshadow/tests/test_transformers/test_internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,16 @@ def test_drop_transformer_below_thresh():
x = pd.DataFrame({"A": np.array([np.nan] * 8 + [0.1, 0.1])})

assert DropFeature().fit_transform(x).values.size == 0


def test_drop_transformer_string_input():
import uuid
import numpy as np
import pandas as pd
from foreshadow.transformers.internals import DropFeature

x = pd.DataFrame({"A": np.array([str(uuid.uuid4()) for _ in range(40)])})

assert np.array_equal(
x.values.ravel(), DropFeature().fit_transform(x).values.ravel()
)
8 changes: 5 additions & 3 deletions foreshadow/transformers/internals/dropfeature.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
import pandas as pd
from sklearn.base import TransformerMixin, BaseEstimator
from sklearn.utils import check_array
from sklearn.utils.validation import check_is_fitted
Expand All @@ -22,8 +23,9 @@ def fit(self, X, y=None):
self
"""
X = check_array(X, force_all_finite=False).ravel()
ratio = np.count_nonzero(np.isfinite(X)) / X.size
X = check_array(X, force_all_finite=False, dtype=None).ravel()
ratio = np.count_nonzero(~pd.isnull(X)) / X.size

self.drop_ = ratio < self.threshold
return self

Expand All @@ -37,6 +39,6 @@ def transform(self, X, y=None):
:obj:`pandas.DataFrame`: Transformed data
"""
X = check_array(X, force_all_finite=False, copy=True)
X = check_array(X, force_all_finite=False, dtype=None, copy=True)
check_is_fitted(self, "drop_")
return X if not self.drop_ else np.array([])

0 comments on commit 4254249

Please sign in to comment.