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?