numpy.ma.assert_array_equal#

ma.assert_array_equal(actual, desired, err_msg='', verbose=True, *, strict=False)[source]#

Raises an AssertionError if two masked arrays are not equal.

Given two array_like objects, check that their masks are equal and that their data is equal wherever it is not masked. Unlike numpy.testing.assert_array_equal, which ignores masked values, a value that is masked in only one of the objects is reported as a mismatch. Objects that are not masked arrays are treated as having no masked values. As in numpy.testing.assert_array_equal, NaNs in the same unmasked positions are considered equal, and a scalar is compared with every element of an array unless strict is set.

Added in version 2.6.0.

Parameters:
actualarray_like

The actual object to check.

desiredarray_like

The desired, expected object.

err_msgstr, optional

The error message to be printed in case of failure.

verbosebool, optional

If True, the conflicting values are appended to the error message.

strictbool, optional

If True, raise an AssertionError when either the shape or the data type of the array_like objects does not match, and disable the special handling of scalars.

Raises:
AssertionError

If the masks of actual and desired, or their unmasked values, are not equal.

See also

numpy.testing.assert_array_equal

Ignores values masked in either array.

numpy.ma.allequal

Examples

>>> import numpy as np

Masked values are not compared, so this does not raise:

>>> a = np.ma.array([1.0, 2.0, 3.0], mask=[False, True, False])
>>> b = np.ma.array([1.0, -9.0, 3.0], mask=[False, True, False])
>>> np.ma.assert_array_equal(a, b)

A value that is masked in only one of the objects is a mismatch:

>>> np.ma.assert_array_equal(a, [1.0, 2.0, 3.0])
Traceback (most recent call last):
    ...
AssertionError:
Arrays are not equal

Mismatched mask elements: 1 / 3 (33.3%)
Mask mismatch at index:
 [1]: -- (ACTUAL), 2.0 (DESIRED)
 ACTUAL: masked_array(data=[1.0, --, 3.0],
             mask=[False,  True, False],
       fill_value=1e+20)
 DESIRED: masked_array(data=[1., 2., 3.],
             mask=False,
       fill_value=1e+20)