Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

165
Views
numpy.testing.assert_array_equal fails with two identical ragged arrays of arrays

I have two numpy arrays and I want to test for equality.

The following works correctly:

# this works
x = np.array([np.array(['a', 'b']), np.array(['c', 'd'])], dtype='object')
y = np.array([np.array(['a', 'b']), np.array(['c', 'd'])], dtype='object')
assert np.testing.assert_array_equal(x,y)

If one of the internal arrays is ragged however, comparison fails:

# this works
x = np.array([np.array(['a', 'b']), np.array(['c'])], dtype='object')
y = np.array([np.array(['a', 'b']), np.array(['c'])], dtype='object')
np.testing.assert_array_equal(x,y)

Traceback (most recent call last):
  File "/home/.../test.py", line 12, in <module>
    np.testing.assert_array_equal(x,y)
  File "/home/.../lib/python3.9/site-packages/numpy/testing/_private/utils.py", line 932, in assert_array_equal
    assert_array_compare(operator.__eq__, x, y, err_msg=err_msg,
  File "/home/.../lib/python3.9/site-packages/numpy/testing/_private/utils.py", line 842, in assert_array_compare
    raise AssertionError(msg)
AssertionError: 
Arrays are not equal

Mismatched elements: 1 / 1 (100%)
 x: array([array(['a', 'b'], dtype='<U1'), array(['c'], dtype='<U1')],
      dtype=object)
 y: array([array(['a', 'b'], dtype='<U1'), array(['c'], dtype='<U1')],
      dtype=object)

UPDATE:

To make the story even more obscure, the following works:

x = np.array([np.array(['a', 'b']), np.array(['c'])], dtype='object')
y = x
np.testing.assert_array_equal(x,y)

Is this the correct behaviour?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

In the first case, the arrays are (2,2) (despite the object dtype):

In [20]: x = np.array([np.array(['a', 'b']), np.array(['c', 'd'])], dtype='object')
    ...: y = np.array([np.array(['a', 'b']), np.array(['c', 'd'])], dtype='object')
In [21]: x
Out[21]: 
array([['a', 'b'],
       ['c', 'd']], dtype=object)
In [22]: x.shape
Out[22]: (2, 2)
In [23]: x==y
Out[23]: 
array([[ True,  True],
       [ True,  True]])

The assert just has to verify that all elements of this comparison are True

The second case:

In [24]: x = np.array([np.array(['a', 'b']), np.array(['c'])], dtype='object')
    ...: y = np.array([np.array(['a', 'b']), np.array(['c'])], dtype='object')
In [25]: x
Out[25]: 
array([array(['a', 'b'], dtype='<U1'), array(['c'], dtype='<U1')],
      dtype=object)
In [26]: x.shape
Out[26]: (2,)
In [27]: x==y
<ipython-input-27-051436df861e>:1: DeprecationWarning: elementwise comparison failed; 
 this will raise an error in the future.
  x==y
Out[27]: False

The result is a scalar, not a (2,) array. x==x produces True, with the same warning.

The array elements could be compared pairwise:

In [30]: [i==j for i,j in zip(x,y)]
Out[30]: [array([ True,  True]), array([ True])]
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!