8000 BUG: Array comparison between numerical and non-numerical returns builtin bool · Issue #27271 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Array comparison between numerical and non-numerical returns builtin bool #27271

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

Closed
n-gao opened this issue Aug 23, 2024 · 1 comment · Fixed by #27275
Closed

BUG: Array comparison between numerical and non-numerical returns builtin bool #27271

n-gao opened this issue Aug 23, 2024 · 1 comment · Fixed by #27275

Comments

@n-gao
Copy link
n-gao commented Aug 23, 2024

Describe the issue:

Comparing two arrays where one is a string and the other a numerical value returns a builtin bool. This causes the np.array_equal method to fail as no .all method exists.

Reproduce the code example:

import numpy as np

a = np.array("foo")
b = np.array(1)
np.array_equal(a, b)

Error message:

a1 = array('foo', dtype='<U3'), a2 = array(1), equal_nan = False

    @array_function_dispatch(_array_equal_dispatcher)
    def array_equal(a1, a2, equal_nan=False):
        """
        True if two arrays have the same shape and elements, False otherwise.
    
        Parameters
        ----------
        a1, a2 : array_like
            Input arrays.
        equal_nan : bool
            Whether to compare NaN's as equal. If the dtype of a1 and a2 is
            complex, values will be considered equal if either the real or the
            imaginary component of a given value is ``nan``.
    
            .. versionadded:: 1.19.0
    
        Returns
        -------
        b : bool
            Returns True if the arrays are equal.
    
        See Also
        --------
        allclose: Returns True if two arrays are element-wise equal within a
                  tolerance.
        array_equiv: Returns True if input arrays are shape consistent and all
                     elements equal.
    
        Examples
        --------
        >>> import numpy as np
    
        >>> np.array_equal([1, 2], [1, 2])
        True
    
        >>> np.array_equal(np.array([1, 2]), np.array([1, 2]))
        True
    
        >>> np.array_equal([1, 2], [1, 2, 3])
        False
    
        >>> np.array_equal([1, 2], [1, 4])
        False
    
        >>> a = np.array([1, np.nan])
        >>> np.array_equal(a, a)
        False
    
        >>> np.array_equal(a, a, equal_nan=True)
        True
    
        When ``equal_nan`` is True, complex values with nan components are
        considered equal if either the real *or* the imaginary components are nan.
    
        >>> a = np.array([1 + 1j])
        >>> b = a.copy()
        >>> a.real = np.nan
        >>> b.imag = np.nan
        >>> np.array_equal(a, b, equal_nan=True)
        True
        """
        try:
            a1, a2 = asarray(a1), asarray(a2)
        except Exception:
            return False
        if a1.shape != a2.shape:
            return False
        if not equal_nan:
>           return builtins.bool((a1 == a2).all())
E           AttributeError: 'bool' object has no attribute 'all'

.venv/lib/python3.12/site-packages/numpy/_core/numeric.py:2557: AttributeError

Python and NumPy Versions:

2.1.0
3.12.5 (main, Aug 14 2024, 05:08:31) [Clang 18.1.8 ]

Runtime Environment:

[{'numpy_version': '2.1.0',
'python': '3.12.5 (main, Aug 14 2024, 05:08:31) [Clang 18.1.8 ]',
'uname': uname_result(system='Linux', node='gpu06', release='5.15.0-107-generic', version='#117-Ubuntu SMP Fri Apr 26 12:26:49 UTC 2024', machine='x86_64')},
{'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
'found': ['SSSE3',
'SSE41',
'POPCNT',
'SSE42',
'AVX',
'F16C',
'FMA3',
'AVX2'],
'not_found': ['AVX512F',
'AVX512CD',
'AVX512_KNL',
'AVX512_KNM',
'AVX512_SKX',
'AVX512_CLX',
'AVX512_CNL',
'AVX512_ICL']}},
{'architecture': 'Haswell',
'filepath': '/ceph/ssd/staff/gaoni/repos/sacred/.venv/lib/python3.12/site-packages/numpy.libs/libscipy_openblas64_-ff651d7f.so',
'internal_api': 'openblas',
'num_threads': 20,
'prefix': 'libscipy_openblas',
'threading_layer': 'pthreads',
'user_api': 'blas',
'version': '0.3.27'}]

Context for the issue:

No response

@seberg
Copy link
Member
seberg commented Aug 23, 2024

Thanks for the report, this is indeed an unintentional change and unfortunately already included in numpy 2.0.1.

We should get this fixed for 2.0.1 and 2.1.1.

seberg pushed a commit that referenced this issue Aug 26, 2024
Mitigates #27271. The underlying issue (an array comparison returning a python bool instead of a numpy bool) is not addressed.

The order of statements is slightly reordered, so that the if a1 is a2: check can be done before the calculation of cannot_have_nan
charris pushed a commit to charris/numpy that referenced this issue Aug 26, 2024
Backport of numpy#27275

Mitigates numpy#27271. The underlying issue (an array comparison returning a
python bool instead of a numpy bool) is not addressed.

The order of statements is slightly reordered, so that the if
`a1 is a2:` check can be done before the calculation of
`cannot_have_nan`

Closes numpygh-27271
charris pushed a commit to charris/numpy that referenced this issue Aug 26, 2024
Backport of numpy#27275

Mitigates numpy#27271. The underlying issue (an array comparison returning a
python bool instead of a numpy bool) is not addressed.

The order of statements is slightly reordered, so that the if
`a1 is a2:` check can be done before the calculation of
`cannot_have_nan`

Closes numpygh-27271
DimitriPapadopoulos pushed a commit to DimitriPapadopoulos/numpy that referenced this issue Aug 27, 2024
…27275)

Mitigates numpy#27271. The underlying issue (an array comparison returning a python bool instead of a numpy bool) is not addressed.

The order of statements is slightly reordered, so that the if a1 is a2: check can be done before the calculation of cannot_have_nan
ArvidJB pushed a commit to ArvidJB/numpy that referenced this issue Nov 1, 2024
…27275)

Mitigates numpy#27271. The underlying issue (an array comparison returning a python bool instead of a numpy bool) is not addressed.

The order of statements is slightly reordered, so that the if a1 is a2: check can be done before the calculation of cannot_have_nan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants
0