Skip to content

fix assert_equal for DataTree #10440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ Bug fixes
By `Deepak Cherian <https://github.com/dcherian>`_.
- Check and fix character array string dimension names, issue warnings as needed (:issue:`6352`, :pull:`10395`).
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_.

- Fix the error message of :py:func:`testing.assert_equal` when two different :py:class:`DataTree` objects
are passed (:pull:`10440`). By `Mathias Hauser <https://github.com/mathause>`_.


Documentation
Expand Down Expand Up @@ -1007,7 +1008,7 @@ New Features
for example, will retain the object. However, one cannot do operations that are not possible on the ``ExtensionArray``
then, such as broadcasting. (:issue:`5287`, :issue:`8463`, :pull:`8723`)
By `Ilan Gold <https://github.com/ilan-gold>`_.
- :py:func:`testing.assert_allclose`/:py:func:`testing.assert_equal` now accept a new argument ``check_dims="transpose"``, controlling whether a transposed array is considered equal. (:issue:`5733`, :pull:`8991`)
- :py:func:`testing.assert_allclose` / :py:func:`testing.assert_equal` now accept a new argument ``check_dims="transpose"``, controlling whether a transposed array is considered equal. (:issue:`5733`, :pull:`8991`)
By `Ignacio Martinez Vazquez <https://github.com/ignamv>`_.
- Added the option to avoid automatically creating 1D pandas indexes in :py:meth:`Dataset.expand_dims()`, by passing the new kwarg
``create_index_for_new_dim=False``. (:pull:`8960`)
Expand Down
4 changes: 1 addition & 3 deletions xarray/core/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -1039,15 +1039,13 @@ def diff_dataset_repr(a, b, compat):
def diff_nodewise_summary(a: DataTree, b: DataTree, compat):
"""Iterates over all corresponding nodes, recording differences between data at each location."""

compat_str = _compat_to_str(compat)

summary = []
for path, (node_a, node_b) in group_subtrees(a, b):
a_ds, b_ds = node_a.dataset, node_b.dataset

if not a_ds._all_compat(b_ds, compat):
path_str = "root node" if path == "." else f"node {path!r}"
dataset_diff = diff_dataset_repr(a_ds, b_ds, compat_str)
dataset_diff = diff_dataset_repr(a_ds, b_ds, compat)
data_diff = indent(
"\n".join(dataset_diff.split("\n", 1)[1:]), prefix=" "
)
Expand Down
21 changes: 21 additions & 0 deletions xarray/tests/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,27 @@ def test_diff_datatree_repr_node_data(self):
actual = formatting.diff_datatree_repr(dt_1, dt_2, "identical")
assert actual == expected

def test_diff_datatree_repr_equals(self) -> None:
ds1 = xr.Dataset(data_vars={"data": ("y", [5, 2])})
ds2 = xr.Dataset(data_vars={"data": (("x", "y"), [[5, 2]])})
dt1 = xr.DataTree.from_dict({"node": ds1})
dt2 = xr.DataTree.from_dict({"node": ds2})

expected = dedent(
"""\
Left and right DataTree objects are not equal

Data at node 'node' does not match:
Differing dimensions:
(y: 2) != (x: 1, y: 2)
Differing data variables:
L data (y) int64 16B 5 2
R data (x, y) int64 16B 5 2"""
)

actual = formatting.diff_datatree_repr(dt1, dt2, "equals")
assert actual == expected


def test_inline_variable_array_repr_custom_repr() -> None:
class CustomArray:
Expand Down
Loading