Skip to content

Commit

Permalink
Datatree deduplicate setitem (#9602)
Browse files Browse the repository at this point in the history
* test

* bugfix
  • Loading branch information
TomNicholas authored Oct 11, 2024
1 parent c057d13 commit 70a2a55
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions xarray/core/datatree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
13 changes: 13 additions & 0 deletions xarray/tests/test_datatree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"""
Expand Down

0 comments on commit 70a2a55

Please sign in to comment.