From 70a2a55afb4a73a4d5027506ea87f918949e2b7c Mon Sep 17 00:00:00 2001 From: Tom Nicholas Date: Thu, 10 Oct 2024 20:48:03 -0600 Subject: [PATCH] Datatree deduplicate setitem (#9602) * test * bugfix --- xarray/core/datatree.py | 3 +++ xarray/tests/test_datatree.py | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/xarray/core/datatree.py b/xarray/core/datatree.py index 3b82f7a58d2..e8d96f111e2 100644 --- a/xarray/core/datatree.py +++ b/xarray/core/datatree.py @@ -813,6 +813,9 @@ def _replace_node( if data is not _default: self._set_node_data(ds) + if self.parent is not None: + _deduplicate_inherited_coordinates(self, self.parent) + self.children = children def _copy_node( diff --git a/xarray/tests/test_datatree.py b/xarray/tests/test_datatree.py index 1de53a35311..9681cb2de76 100644 --- a/xarray/tests/test_datatree.py +++ b/xarray/tests/test_datatree.py @@ -1315,6 +1315,19 @@ def test_inherited_coords_with_index_are_deduplicated(self): expected = xr.Dataset({"foo": ("x", [4, 5])}) assert_identical(child_dataset, expected) + def test_deduplicated_after_setitem(self): + # regression test for GH #9601 + dt = DataTree.from_dict( + { + "/": xr.Dataset(coords={"x": [1, 2]}), + "/b": None, + } + ) + dt["b/x"] = dt["x"] + child_dataset = dt.children["b"].to_dataset(inherited=False) + expected = xr.Dataset() + assert_identical(child_dataset, expected) + def test_inconsistent_dims(self): expected_msg = _exact_match( """