datatree: `drop_vars` issue?
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 14
Description
What is your issue?
I don't know if it is an issue (datatree==0.0.14) but, at least, it is an error that could appear quite easily:
import datatree import xarray as xr ds = xr.DataArray([[1,2,3]],dims=['a','b']) dt =datatree.DataTree() dt['A'] = ds dt['B'] = ds for grp in dt.groups: vars2drop = [vv for vv in dt[grp] if 'example' in vv] dt[grp] = dt[grp].drop_vars(vars2drop)the case that would be problematic is:
dt['/'] = dt['/'].drop_vars([])the error raised is:
File ~/micromamba/envs/micromamba1/lib/python3.10/site-packages/datatree/datatree.py:852, in DataTree.__setitem__(self, key, value) 848 elif isinstance(key, str): 849 # TODO should possibly deal with hashables in general? 850 # path-like: a name of a node/variable, or path to a node/variable 851 path = NodePath(key) --> 852 return self._set_item(path, value, new_nodes_along_path=True) 853 else: 854 raise ValueError("Invalid format for key") File ~/micromamba/envs/micromamba1/lib/python3.10/site-packages/datatree/treenode.py:494, in TreeNode._set_item(self, path, item, new_nodes_along_path, allow_overwrite) 491 path = NodePath(path) 493 if not path.name: --> 494 raise ValueError("Can't set an item under a path which has no name") 496 if path.root: 497 # absolute path 498 current_node = self.root ValueError: Can't set an item under a path which has no name
Originally posted by @agrouaze in https://github.com/xarray-contrib/datatree/issues/316#issue-2153781384
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Run the provided DataTree reproduction with datatree 0.0.14, then inspect DataTree.setitem in datatree.py and TreeNode._set_item in treenode.py, where the traceback points. Clarify the expected behavior for assigning the result of drop_vars([]) to the root path; done means the reported operation no longer raises an inappropriate path error and has regression coverage.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- data
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 58/100