Skip to content

Commit bf210fe

Browse files
committed
MNT: Improve comments on assert_array_compare nan/inf handling logic
- Use same language as elsewhere below to explain `!= True` used to handle np.ma.masked - Clarify committed to support standard MaskedArrays - Restore note lost in 7315145 comment changes about how the np.bool casts towards the end of the function handle np.ma.masked, and expand further.
1 parent 00c9d29 commit bf210fe

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

numpy/testing/_private/utils.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -769,14 +769,14 @@ def func_assert_same_pos(x, y, func=isnan, hasval='nan'):
769769
y_id = func(y)
770770
# We include work-arounds here to handle three types of slightly
771771
# pathological ndarray subclasses:
772-
# (1) all() on `masked` array scalars can return masked arrays, so we
773-
# use != True
772+
# (1) all() on fully masked arrays returns np.ma.masked, so we use != True
773+
# (np.ma.masked != True evaluates as np.ma.masked, which is falsy).
774774
# (2) __eq__ on some ndarray subclasses returns Python booleans
775775
# instead of element-wise comparisons, so we cast to np.bool in
776776
# that case.
777777
# (3) subclasses with bare-bones __array_function__ implementations may
778778
# not implement np.all(), so favor using the .all() method
779-
# We are not committed to supporting such subclasses, but it's nice to
779+
# We are not committed to supporting cases (2) and (3), but it's nice to
780780
# support them if possible.
781781
result = x_id == y_id
782782
if isinstance(result, bool):
@@ -791,6 +791,9 @@ def func_assert_same_pos(x, y, func=isnan, hasval='nan'):
791791
raise AssertionError(msg)
792792
# If there is a scalar, then here we know the array has the same
793793
# flag as it everywhere, so we should return the scalar flag.
794+
# np.ma.masked is also handled and converted to np.False_ (even if the other
795+
# array has nans/infs etc.; that's OK given the handling later of fully-masked
796+
# results).
794797
if isinstance(x_id, bool) or x_id.ndim == 0:
795798
return np.bool(x_id)
796799
elif isinstance(y_id, bool) or y_id.ndim == 0:

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy