Skip to content

[MRG] Fix remove FutureWarning in _object_dtype_isnan #12567

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
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
13 changes: 4 additions & 9 deletions sklearn/utils/fixes.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,18 +309,13 @@ def nanmedian(a, axis=None):
# Fix for behavior inconsistency on numpy.equal for object dtypes.
# For numpy versions < 1.13, numpy.equal tests element-wise identity of objects
# instead of equality. This fix returns the mask of NaNs in an array of
# numerical or object values for all nupy versions.

_nan_object_array = np.array([np.nan], dtype=object)
_nan_object_mask = _nan_object_array != _nan_object_array

if np.array_equal(_nan_object_mask, np.array([True])):
# numerical or object values for all numpy versions.
if np_version < (1, 13):
def _object_dtype_isnan(X):
return X != X

return np.frompyfunc(lambda x: x != x, 1, 1)(X).astype(bool)
else:
def _object_dtype_isnan(X):
return np.frompyfunc(lambda x: x != x, 1, 1)(X).astype(bool)
return X != X


# To be removed once this fix is included in six
Expand Down
16 changes: 16 additions & 0 deletions sklearn/utils/tests/test_fixes.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from sklearn.utils.fixes import nanmedian
from sklearn.utils.fixes import nanpercentile
from sklearn.utils.fixes import _joblib_parallel_args
from sklearn.utils.fixes import _object_dtype_isnan


def test_divide():
Expand Down Expand Up @@ -88,3 +89,18 @@ def test_joblib_parallel_args(monkeypatch, joblib_version):
_joblib_parallel_args(verbose=True)
else:
raise ValueError


@pytest.mark.parametrize("dtype, val", ([object, 1],
[object, "a"],
[float, 1]))
def test_object_dtype_isnan(dtype, val):
X = np.array([[val, np.nan],
[np.nan, val]], dtype=dtype)

expected_mask = np.array([[False, True],
[True, False]])

mask = _object_dtype_isnan(X)

assert_array_equal(mask, expected_mask)