Skip to content

Commit 5f47a03

Browse files
authored
fix assert_equal for DataTree (#10440)
* fix assert_equal for DataTree * test typing * changelog
1 parent 5beccf7 commit 5f47a03

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

doc/whats-new.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ Bug fixes
3232
By `Deepak Cherian <https://github.com/dcherian>`_.
3333
- Check and fix character array string dimension names, issue warnings as needed (:issue:`6352`, :pull:`10395`).
3434
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_.
35-
35+
- Fix the error message of :py:func:`testing.assert_equal` when two different :py:class:`DataTree` objects
36+
are passed (:pull:`10440`). By `Mathias Hauser <https://github.com/mathause>`_.
3637

3738

3839
Documentation
@@ -1009,7 +1010,7 @@ New Features
10091010
for example, will retain the object. However, one cannot do operations that are not possible on the ``ExtensionArray``
10101011
then, such as broadcasting. (:issue:`5287`, :issue:`8463`, :pull:`8723`)
10111012
By `Ilan Gold <https://github.com/ilan-gold>`_.
1012-
- :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`)
1013+
- :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`)
10131014
By `Ignacio Martinez Vazquez <https://github.com/ignamv>`_.
10141015
- Added the option to avoid automatically creating 1D pandas indexes in :py:meth:`Dataset.expand_dims()`, by passing the new kwarg
10151016
``create_index_for_new_dim=False``. (:pull:`8960`)

xarray/core/formatting.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,15 +1039,13 @@ def diff_dataset_repr(a, b, compat):
10391039
def diff_nodewise_summary(a: DataTree, b: DataTree, compat):
10401040
"""Iterates over all corresponding nodes, recording differences between data at each location."""
10411041

1042-
compat_str = _compat_to_str(compat)
1043-
10441042
summary = []
10451043
for path, (node_a, node_b) in group_subtrees(a, b):
10461044
a_ds, b_ds = node_a.dataset, node_b.dataset
10471045

10481046
if not a_ds._all_compat(b_ds, compat):
10491047
path_str = "root node" if path == "." else f"node {path!r}"
1050-
dataset_diff = diff_dataset_repr(a_ds, b_ds, compat_str)
1048+
dataset_diff = diff_dataset_repr(a_ds, b_ds, compat)
10511049
data_diff = indent(
10521050
"\n".join(dataset_diff.split("\n", 1)[1:]), prefix=" "
10531051
)

xarray/tests/test_formatting.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,27 @@ def test_diff_datatree_repr_node_data(self):
721721
actual = formatting.diff_datatree_repr(dt_1, dt_2, "identical")
722722
assert actual == expected
723723

724+
def test_diff_datatree_repr_equals(self) -> None:
725+
ds1 = xr.Dataset(data_vars={"data": ("y", [5, 2])})
726+
ds2 = xr.Dataset(data_vars={"data": (("x", "y"), [[5, 2]])})
727+
dt1 = xr.DataTree.from_dict({"node": ds1})
728+
dt2 = xr.DataTree.from_dict({"node": ds2})
729+
730+
expected = dedent(
731+
"""\
732+
Left and right DataTree objects are not equal
733+
734+
Data at node 'node' does not match:
735+
Differing dimensions:
736+
(y: 2) != (x: 1, y: 2)
737+
Differing data variables:
738+
L data (y) int64 16B 5 2
739+
R data (x, y) int64 16B 5 2"""
740+
)
741+
742+
actual = formatting.diff_datatree_repr(dt1, dt2, "equals")
743+
assert actual == expected
744+
724745

725746
def test_inline_variable_array_repr_custom_repr() -> None:
726747
class CustomArray:

0 commit comments

Comments
 (0)