8000 ENH: change object-array comparisons to prefer OO->O unfuncs by mattip · Pull Request #14800 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: change object-array comparisons to prefer OO->O unfuncs #14800

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 3 commits into from
Nov 5, 2019
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
14 changes: 14 additions & 0 deletions doc/release/upcoming_changes/14800.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Comparison on ``object`` dtypes will prefer ``object`` output
-------------------------------------------------------------
Comparison ufuncs (``np.equal`` and friends) would return boolean arrays when
the input array dtype was ``object``. This led to inconsistent behaviour for
ragged arrays ``a = np.array([1, np.array([1, 2, 3])], dtype=object)``. This
will now return an object array::

>>> a = np.array([1, np.array([1, 2, 3])], dtype=object)
>>> np.equal(a, a)
array([True, array([ True, True, True])], dtype=object)

The old behaviour, which will raise a ``ValueError`` in this case, is still
available by specifying a dtype as ``np.equal(a, a, dtype=bool)``.

29 changes: 18 additions & 11 deletions numpy/core/code_generators/generate_umath.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def english_upper(s):
'P': 'OBJECT',
}

all = '? 8000 bBhHiIlLqQefdgFDGOMm'
noobj = '?bBhHiIlLqQefdgFDGmM'
O = 'O'
P = 'P'
ints = 'bBhHiIlLqQ'
Expand All @@ -246,10 +246,8 @@ def english_upper(s):
noint = inexact+O
nointP = inexact+P
allP = bints+times+flts+cmplxP
nobool = all[1:]
noobj = all[:-3]+all[-2:]
nobool_or_obj = all[1:-3]+all[-2:]
nobool_or_datetime = all[1:-2]+all[-1:]
nobool_or_obj = noobj[1:]
nobool_or_datetime = noobj[1:-1] + O # includes m - timedelta64
intflt = ints+flts
intfltcmplx = ints+flts+cmplx
nocmplx = bints+times+flts
Expand Down Expand Up @@ -431,64 +429,73 @@ def english_upper(s):
Ufunc(2, 1, None,
docstrings.get('numpy.core.umath.greater'),
'PyUFunc_SimpleBinaryComparisonTypeResolver',
TD(all, out='?', simd=[('avx2', ints)]),
TD(noobj, out='?', simd=[('avx2', ints)]),
[TypeDescription('O', FullTypeDescr, 'OO', 'O')],
TD('O', out='?'),
),
'greater_equal':
Ufunc(2, 1, None,
docstrings.get('numpy.core.umath.greater_equal'),
'PyUFunc_SimpleBinaryComparisonTypeResolver',
TD(all, out='?', simd=[('avx2', ints)]),
TD(noobj, out='?', simd=[('avx2', ints)]),
[TypeDescription('O', FullTypeDescr, 'OO', 'O')],
TD('O', out='?'),
),
'less':
Ufunc(2, 1, None,
docstrings.get('numpy.core.umath.less'),
'PyUFunc_SimpleBinaryComparisonTypeResolver',
TD(all, out='?', simd=[('avx2', ints)]),
TD(noobj, out='?', simd=[('avx2', ints)]),
[TypeDescription('O', FullTypeDescr, 'OO', 'O')],
TD('O', out='?'),
),
'less_equal':
Ufunc(2, 1, None,
docstrings.get('numpy.core.umath.less_equal'),
'PyUFunc_SimpleBinaryComparisonTypeResolver',
TD(all, out='?', simd=[('avx2', ints)]),
TD(noobj, out='?', simd=[('avx2', ints)]),
[TypeDescription('O', FullTypeDescr, 'OO', 'O')],
TD('O', out='?'),
),
'equal':
Ufunc(2, 1, None,
docstrings.get('numpy.core.umath.equal'),
'PyUFunc_SimpleBinaryComparisonTypeResolver',
TD(all, out='?', simd=[('avx2', ints)]),
TD(noobj, out='?', simd=[('avx2', ints)]),
[TypeDescription('O', FullTypeDescr, 'OO', 'O')],
TD('O', out='?'),
),
'not_equal':
Ufunc(2, 1, None,
docstrings.get('numpy.core.umath.not_equal'),
'PyUFunc_SimpleBinaryComparisonTypeResolver',
TD(all, out='?', simd=[('avx2', ints)]),
TD(noobj, out='?', simd=[('avx2', ints)]),
[TypeDescription('O', FullTypeDescr, 'OO', 'O')],
TD('O', out='?'),
),
'logical_and':
Ufunc(2, 1, True_,
docstrings.get('numpy.core.umath.logical_and'),
'PyUFunc_SimpleBinaryComparisonTypeResolver',
TD(nodatetime_or_obj, out='?', simd=[('avx2', ints)]),
TD(O, f='npy_ObjectLogicalAnd'),
TD(O, f='npy_ObjectLogicalAnd', out='?'),
),
'logical_not':
Ufunc(1, 1, None,
docstrings.get('numpy.core.umath.logical_not'),
None,
TD(nodatetime_or_obj, out='?', simd=[('avx2', ints)]),
TD(O, f='npy_ObjectLogicalNot'),
TD(O, f='npy_ObjectLogicalNot', out='?'),
),
'logical_or':
Ufunc(2, 1, False_,
docstrings.get('numpy.core.umath.logical_or'),
'PyUFunc_SimpleBinaryComparisonTypeResolver',
TD(nodatetime_or_obj, out='?', simd=[('avx2', ints)]),
TD(O, f='npy_ObjectLogicalOr'),
TD(O, f='npy_ObjectLogicalOr', out='?'),
),
'logical_xor':
Ufunc(2, 1, False_,
Expand Down
5 changes: 3 additions & 2 deletions numpy/core/tests/test_deprecations.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,11 @@ def test_normal_types(self):
# (warning is issued a couple of times here)
self.assert_deprecated(op, args=(a, a[:-1]), num=None)

# Element comparison error (numpy array can't be compared).
# ragged array comparison returns True/False
a = np.array([1, np.array([1,2,3])], dtype=object)
b = np.array([1, np.array([1,2,3])], dtype=object)
self.assert_deprecated(op, args=(a, b), num=None)
res = op(a, b)
assert res.dtype == 'object'
Copy link
Member Author

Choose a reason for hiding this comment

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

This now returns an object array instead of deprecating.

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
assert res.dtype == 'object'
assert res.dtype == object


def test_string(self):
# For two string arrays, strings always raised the broadcasting error:
Expand Down
14 changes: 9 additions & 5 deletions numpy/core/tests/test_ufunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1090,14 +1090,18 @@ def __eq__(self, other):
return '=='

arr0d = np.array(HasComparisons())
assert_equal(arr0d == arr0d, True)
assert_equal(np.equal(arr0d, arr0d), True) # normal behavior is a cast
assert_equal(arr0d == arr0d, '==')
assert_equal(np.equal(arr0d, arr0d), '==')
assert_equal(np.equal(arr0d, arr0d, dtype=bool), True)
assert_equal(np.equal(arr0d, arr0d, dtype=object), '==')

arr1d = np.array([HasComparisons()])
assert_equal(arr1d == arr1d, np.array([True]))
assert_equal(np.equal(arr1d, arr1d), np.array([True])) # normal behavior is a cast
assert_equal(np.equal(arr1d, arr1d, dtype=object), np.array(['==']))
ret_obj = np.array(['=='], dtype=object)
ret_bool = np.array([True])
assert_equal(arr1d == arr1d, ret_obj)
assert_equal(np.equal(arr1d, arr1d), ret_obj)
assert_equal(np.equal(arr1d, arr1d, dtype=object), ret_obj)
assert_equal(np.equal(arr1d, arr1d, dtype=bool), ret_bool)

def test_object_array_reduction(self):
# Reductions on object arrays
Expand Down
10 changes: 6 additions & 4 deletions numpy/core/tests/test_umath.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,11 @@ def __array_wrap__(self, arr, context):

class TestComparisons(object):
def test_ignore_object_identity_in_equal(self):
# Check error raised when comparing identical objects whose comparison
# Check comparing identical objects whose comparison
# is not a simple boolean, e.g., arrays that are compared elementwise.
a = np.array([np.array([1, 2, 3]), None], dtype=object)
assert_raises(ValueError, np.equal, a, a)
b = np.equal(a, a.copy())
assert b.shape == a.shape

# Check error raised when comparing identical non-comparable objects.
class FunkyType(object):
Expand All @@ -188,10 +189,11 @@ def __eq__(self, other):
assert_equal(np.equal(a, a), [False])

def test_ignore_object_identity_in_not_equal(self):
# Check error raised when comparing identical objects whose comparison
# Check comparing identical objects whose comparison
# is not a simple boolean, e.g., arrays that are compared elementwise.
a = np.array([np.array([1, 2, 3]), None], dtype=object)
assert_raises(ValueError, np.not_equal, a, a)
b = np.not_equal(a, a.copy())
assert b.shape == a.shape

# Check error raised when comparing identical non-comparable objects.
class FunkyType(object):
Expand Down
8 changes: 6 additions & 2 deletions numpy/lib/arraysetops.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,11 +562,15 @@ def in1d(ar1, ar2, assume_unique=False, invert=False):
if invert:
mask = np.ones(len(ar1), dtype=bool)
for a in ar2:
mask &= (ar1 != a)
# convert object arrays to bool
# cannot use np.not_equal until 'S' and 'U' have loops
mask &= (ar1 != a).astype(bool)
else:
mask = np.zeros(len(ar1), dtype=bool)
for a in ar2:
mask |= (ar1 == a)
# convert object arrays to bool
# cannot use np.equal until 'S' and 'U' have loops
mask |= (ar1 == a).astype(bool)
return mask

# Otherwise use sorting
Expand Down
2 changes: 1 addition & 1 deletion numpy/lib/nanfunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def _replace_nan(a, val):

if a.dtype == np.object_:
# object arrays do not support `isnan` (gh-9009), so make a guess
mask = a != a
mask = np.not_equal(a, a, dtype=bool)
elif issubclass(a.dtype.type, np.inexact):
mask = np.isnan(a)
else:
Expand Down
7 changes: 3 additions & 4 deletions numpy/linalg/tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,9 @@ def test_norm_object_array(self):
assert_raises(ValueError, linalg.norm, testvector, ord='nuc')
assert_raises(ValueError, linalg.norm, testvector, ord=np.inf)
assert_raises(ValueError, linalg.norm, testvector, ord=-np.inf)
with warnings.catch_warnings():
warnings.simplefilter("error", DeprecationWarning)
assert_raises((AttributeError, DeprecationWarning),
linalg.norm, testvector, ord=0)
# Succeeds, equivalent to "sum(x != 0)"
r = linalg.norm(testvector, ord=0)
assert_(r.dtype == 'bool')
assert_raises(ValueError, linalg.norm, testvector, ord=-1)
assert_raises(ValueError, linalg.norm, testvector, ord=-2)

Expand Down
7 changes: 6 additions & 1 deletion numpy/ma/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4790,7 +4790,12 @@ def all(self, axis=None, out=None, keepdims=np._NoValue):

mask = _check_mask_axis(self._mask, axis, **kwargs)
if out is None:
d = self.filled(True).all(axis=axis, **kwargs).view(type(self))
r = self.filled(True).all(axis=axis, **kwargs)
# object dtypes with axis=None return a scalar
if isinstance(r, bool):
d = type(self)(r)
else:
d = r.view(type(self))
Copy link
Member Author

Choose a reason for hiding this comment

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

since all is np.logical_and.reduce, the OO->O mapping reduces the output to a boolean scalar, which has no view.

Copy link
Member

Choose a reason for hiding this comment

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

Alternatively, could pass keepdims=True, and then throw away the dimension at the end

if d.ndim:
d.__setmask__(mask)
elif mask:
Expand Down
0