Like :py:func:`numpy.testing.assert_array_equal`, but for xarray objects. Raises an AssertionError if two objects are not equal. This will match data values, dimensions and coordinates, but not names or attributes (except for Dataset objects for which the variable names must match).
(a, b, check_dim_order: bool = True)
| 110 | |
| 111 | @ensure_warnings |
| 112 | def assert_equal(a, b, check_dim_order: bool = True): |
| 113 | """Like :py:func:`numpy.testing.assert_array_equal`, but for xarray |
| 114 | objects. |
| 115 | |
| 116 | Raises an AssertionError if two objects are not equal. This will match |
| 117 | data values, dimensions and coordinates, but not names or attributes |
| 118 | (except for Dataset objects for which the variable names must match). |
| 119 | Arrays with NaN in the same location are considered equal. |
| 120 | |
| 121 | For DataTree objects, assert_equal is mapped over all Datasets on each node, |
| 122 | with the DataTrees being equal if both are isomorphic and the corresponding |
| 123 | Datasets at each node are themselves equal. |
| 124 | |
| 125 | Parameters |
| 126 | ---------- |
| 127 | a : xarray.Dataset, xarray.DataArray, xarray.Variable, xarray.Coordinates |
| 128 | or xarray.core.datatree.DataTree. The first object to compare. |
| 129 | b : xarray.Dataset, xarray.DataArray, xarray.Variable, xarray.Coordinates |
| 130 | or xarray.core.datatree.DataTree. The second object to compare. |
| 131 | check_dim_order : bool, optional, default is True |
| 132 | Whether dimensions must be in the same order. |
| 133 | |
| 134 | See Also |
| 135 | -------- |
| 136 | assert_identical, assert_allclose, Dataset.equals, DataArray.equals |
| 137 | numpy.testing.assert_array_equal |
| 138 | """ |
| 139 | __tracebackhide__ = True |
| 140 | assert type(a) is type(b) or ( |
| 141 | isinstance(a, Coordinates) and isinstance(b, Coordinates) |
| 142 | ) |
| 143 | b = maybe_transpose_dims(a, b, check_dim_order) |
| 144 | if isinstance(a, Variable | DataArray): |
| 145 | assert a.equals(b), formatting.diff_array_repr(a, b, "equals") |
| 146 | elif isinstance(a, Dataset): |
| 147 | assert a.equals(b), formatting.diff_dataset_repr(a, b, "equals") |
| 148 | elif isinstance(a, Coordinates): |
| 149 | assert a.equals(b), formatting.diff_coords_repr(a, b, "equals") |
| 150 | elif isinstance(a, DataTree): |
| 151 | assert a.equals(b), diff_datatree_repr(a, b, "equals") |
| 152 | else: |
| 153 | raise TypeError(f"{type(a)} not supported by assertion comparison") |
| 154 | |
| 155 | |
| 156 | @ensure_warnings |
searching dependent graphs…