10000 ENH: add OO->? loops, use np.compare(a, b, dtype=bool), add comments · numpy/numpy@5c77895 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5c77895

Browse files
committed
ENH: add OO->? loops, use np.compare(a, b, dtype=bool), add comments
1 parent 647ea19 commit 5c77895

File tree

6 files changed

+26
-8
lines changed

6 files changed

+26
-8
lines changed

numpy/core/code_generators/generate_umath.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,62 +431,71 @@ def english_upper(s):
431431
'PyUFunc_SimpleBinaryComparisonTypeResolver',
432432
TD(noobj, out='?', simd=[('avx2', ints)]),
433433
[TypeDescription('O', FullTypeDescr, 'OO', 'O')],
434+
TD('O', out='?'),
434435
),
435436
'greater_equal':
436437
Ufunc(2, 1, None,
437438
docstrings.get('numpy.core.umath.greater_equal'),
438439
'PyUFunc_SimpleBinaryComparisonTypeResolver',
439440
TD(noobj, out='?', simd=[('avx2', ints)]),
440441
[TypeDescription('O', FullTypeDescr, 'OO', 'O')],
442+
TD('O', out='?'),
441443
),
442444
'less':
443445
Ufunc(2, 1, None,
444446
docstrings.get('numpy.core.umath.less'),
445447
'PyUFunc_SimpleBinaryComparisonTypeResolver',
446448
TD(noobj, out='?', simd=[('avx2', ints)]),
447449
[TypeDescription('O', FullTypeDescr, 'OO', 'O')],
450+
TD('O', out='?'),
448451
),
449452
'less_equal':
450453
Ufunc(2, 1, None,
451454
docstrings.get('numpy.core.umath.less_equal'),
452455
'PyUFunc_SimpleBinaryComparisonTypeResolver',
453456
TD(noobj, out='?', simd=[('avx2', ints)]),
454457
[TypeDescription('O', FullTypeDescr, 'OO', 'O')],
458+
TD('O', out='?'),
455459
),
456460
'equal':
457461
Ufunc(2, 1, None,
458462
docstrings.get('numpy.core.umath.equal'),
459463
'PyUFunc_SimpleBinaryComparisonTypeResolver',
460464
TD(noobj, out='?', simd=[('avx2', ints)]),
461465
[TypeDescription('O', FullTypeDescr, 'OO', 'O')],
466+
TD('O', out='?'),
462467
),
463468
'not_equal':
464469
Ufunc(2, 1, None,
465470
docstrings.get('numpy.core.umath.not_equal'),
466471
'PyUFunc_SimpleBinaryComparisonTypeResolver',
467472
TD(noobj, out='?', simd=[('avx2', ints)]),
468473
[TypeDescription('O', FullTypeDescr, 'OO', 'O')],
474+
TD('O', out='?'),
469475
),
470476
'logical_and':
471477
Ufunc(2, 1, True_,
472478
docstrings.get('numpy.core.umath.logical_and'),
473479
'PyUFunc_SimpleBinaryComparisonTypeResolver',
474480
TD(nodatetime_or_obj, out='?', simd=[('avx2', ints)]),
475481
TD(O, f='npy_ObjectLogicalAnd'),
482+
TD(O, f='npy_ObjectLogicalAnd', out='?'),
476483
),
477484
'logical_not':
478485
Ufunc(1, 1, None,
479486
docstrings.get('numpy.core.umath.logical_not'),
480487
None,
481488
TD(nodatetime_or_obj, out='?', simd=[('avx2', ints)]),
482489
TD(O, f='npy_ObjectLogicalNot'),
490+
TD(O, f='npy_ObjectLogicalNot', out='?'),
483491
),
484492
'logical_or':
485493
Ufunc(2, 1, False_,
486494
docstrings.get('numpy.core.umath.logical_or'),
487495
'PyUFunc_SimpleBinaryComparisonTypeResolver',
488496
TD(nodatetime_or_obj, out='?', simd=[('avx2', ints)]),
489497
TD(O, f='npy_ObjectLogicalOr'),
498+
TD(O, f='npy_ObjectLogicalOr', out='?'),
490499
),
491500
'logical_xor':
492501
Ufunc(2, 1, False_,

numpy/core/tests/test_ufunc.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,14 +1090,18 @@ def __eq__(self, other):
10901090
return '=='
10911091

10921092
arr0d = np.array(HasComparisons())
1093-
assert_equal(arr0d == arr0d, True)
1094-
assert_equal(np.equal(arr0d, arr0d), True) # normal behavior is a cast
1093+
assert_equal(arr0d == arr0d, '==')
1094+
assert_equal(np.equal(arr0d, arr0d), '==')
1095+
assert_equal(np.equal(arr0d, arr0d, dtype=bool), True)
10951096
assert_equal(np.equal(arr0d, arr0d, dtype=object), '==')
10961097

10971098
arr1d = np.array([HasComparisons()])
1098-
assert_equal(arr1d == arr1d, np.array([True]))
1099-
assert_equal(np.equal(arr1d, arr1d), np.array([True])) # normal behavior is a cast
1100-
assert_equal(np.equal(arr1d, arr1d, dtype=object), np.array(['==']))
1099+
ret_obj = np.array(['=='], dtype=object)
1100+
ret_bool = np.array([True])
1101+
assert_equal(arr1d == arr1d, ret_obj)
1102+
assert_equal(np.equal(arr1d, arr1d), ret_obj)
1103+
assert_equal(np.equal(arr1d, arr1d, dtype=object), ret_obj)
1104+
assert_equal(np.equal(arr1d, arr1d, dtype=bool), ret_bool)
11011105

11021106
def test_object_array_reduction(self):
11031107
# Reductions on object arrays

numpy/lib/arraysetops.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,10 +562,14 @@ def in1d(ar1, ar2, assume_unique=False, invert=False):
562562
if invert:
563563
mask = np.ones(len(ar1), dtype=bool)
564564
for a in ar2:
565+
# convert object arrays to bool
566+
# cannot use np.not_equal until 'S' and 'U' have loops
565567
mask &= (ar1 != a).astype(bool)
566568
else:
567569
mask = np.zeros(len(ar1), dtype=bool)
568570
for a in ar2:
571+
# convert object arrays to bool
572+
# cannot use np.equal until 'S' and 'U' have loops
569573
mask |= (ar1 == a).astype(bool)
570574
return mask
571575

numpy/lib/nanfunctions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def _replace_nan(a, val):
9999

100100
if a.dtype == np.object_:
101101
# object arrays do not support `isnan` (gh-9009), so make a guess
102-
mask = (a != a).astype(bool)
102+
mask = np.not_equal(a, a, dtype=bool)
103103
elif issubclass(a.dtype.type, np.inexact):
104104
mask = np.isnan(a)
105105
else:

numpy/linalg/tests/test_regression.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ def test_norm_object_array(self):
109109
assert_raises(ValueError, linalg.norm, testvector, ord='nuc')
110110
assert_raises(ValueError, linalg.norm, testvector, ord=np.inf)
111111
assert_raises(ValueError, linalg.norm, testvector, ord=-np.inf)
112-
# Succeeds, but returns boolean?
112+
# Succeeds, equivalent to "sum(x != 0)"
113113
r = linalg.norm(testvector, ord=0)
114-
assert_(r.dtype == np.type('float64'))
114+
assert_(r.dtype == 'bool')
115115
assert_raises(ValueError, linalg.norm, testvector, ord=-1)
116116
assert_raises(ValueError, linalg.norm, testvector, ord=-2)
117117

numpy/ma/core.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4791,6 +4791,7 @@ def all(self, axis=None, out=None, keepdims=np._NoValue):
47914791
mask = _check_mask_axis(self._mask, axis, **kwargs)
47924792
if out is None:
47934793
r = self.filled(True).all(axis=axis, **kwargs)
4794+
# object dtypes with axis=None return a scalar
47944795
if isinstance(r, bool):
47954796
d = type(self)(r)
47964797
else:

0 commit comments

Comments
 (0)
0