Skip to content

Commit

Permalink
Datatree setitem dataset (#9516)
Browse files Browse the repository at this point in the history
* un-xfail tests

* wrap Dataset input in a DataTree node before setting
  • Loading branch information
TomNicholas committed Sep 19, 2024
1 parent 17571b5 commit a74ddf7
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 2 additions & 0 deletions xarray/core/datatree.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,8 @@ def __setitem__(
# TODO should possibly deal with hashables in general?
# path-like: a name of a node/variable, or path to a node/variable
path = NodePath(key)
if isinstance(value, Dataset):
value = DataTree(dataset=value)
return self._set_item(path, value, new_nodes_along_path=True)
else:
raise ValueError("Invalid format for key")
Expand Down
2 changes: 0 additions & 2 deletions xarray/tests/test_datatree.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,14 +513,12 @@ def test_setitem_dataset_on_this_node(self):
results["."] = data
assert_identical(results.to_dataset(), data)

@pytest.mark.xfail(reason="assigning Datasets doesn't yet create new nodes")
def test_setitem_dataset_as_new_node(self):
data = xr.Dataset({"temp": [0, 50]})
folder1 = DataTree(name="folder1")
folder1["results"] = data
assert_identical(folder1["results"].to_dataset(), data)

@pytest.mark.xfail(reason="assigning Datasets doesn't yet create new nodes")
def test_setitem_dataset_as_new_node_requiring_intermediate_nodes(self):
data = xr.Dataset({"temp": [0, 50]})
folder1 = DataTree(name="folder1")
Expand Down

0 comments on commit a74ddf7

Please sign in to comment.