Skip to content

DataTree.__delitem__ should support deleting variables, not only child nodes #9221

@shoyer

Description

@shoyer

What happened?

Currently this raises a very uninformative error message:

In [1]: from xarray.core.datatree import DataTree

In [2]: tree = DataTree()

In [3]: tree['x'] = 1

In [4]: tree
Out[4]:
<xarray.DataTree>
Group: /
    Dimensions:  ()
    Data variables:
        x        int64 8B 1

In [5]: del tree['x']
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[5], line 1
----> 1 del tree['x']

File ~/dev/xarray/xarray/core/treenode.py:563, in TreeNode.__delitem__(self, key)
    561     child.orphan()
    562 else:
--> 563     raise KeyError("Cannot delete")

KeyError: 'Cannot delete'

What did you expect to happen?

The variable 'x' should be deleted, consistent with __getitem__. Short of that, we should raise an informative error message about why this isn't possible.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugtopic-DataTreeRelated to the implementation of a DataTree class

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions