10000 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
Closed
@n-gao

Description

@n-gao

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0