-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DataTree: selecting via [["a"]]
suggests subset
, which is missing
#10014
Comments
[["a"]]
suggests subset, which is missing
[["a"]]
suggests subset, which is missing[["a"]]
suggests subset
, which is missing
Xarray issue: DataTree: selecting via
|
return self._copy_listed(key) |
When the passed object is iterable or hashable, it calls _copy_listed
:
Line 1105 in e8b1daf
def _copy_listed(self, names: Iterable[Hashable]) -> Self: |
With DataTree
xdt = xr.DataTree.from_dict({"/": xds})
print(xdt)
<xarray.DataTree>
Group: /
Dimensions: (x: 1)
Dimensions without coordinates: x
Data variables:
a (x) int64 8B 0
print(xdt["a"])
<xarray.DataArray 'a' (x: 1)> Size: 8B
array([0])
Dimensions without coordinates: x
print(xdt[["a"]])
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
Cell In[20], line 1
----> 1 print(xdt[["a"]])
File .venv/lib/python3.13/site-packages/xarray/core/datatree.py:941, in DataTree.__getitem__(self, key)
938 return self._get_item(path)
939 elif utils.is_list_like(key):
940 # iterable of variable names
--> 941 raise NotImplementedError(
942 "Selecting via tags is deprecated, and selecting multiple items should be "
943 "implemented via .subset"
944 )
945 else:
946 raise ValueError(f"Invalid format for key: {key}")
NotImplementedError: Selecting via tags is deprecated, and selecting multiple items should be implemented via .subset
See __getitem__
in the datatree.py
:
xarray/xarray/core/datatree.py
Line 942 in e8b1daf
raise NotImplementedError( |
The error message comes from datatree itself and states:
NotImplementedError: Selecting via tags is deprecated, and selecting multiple items should be implemented via .subset
Trying to use .subset
fails with an AttributeError
:
print(xdt.subset(["a"]))
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[21], line 1
----> 1 print(xdt.subset(["a"]))
File .venv/lib/python3.13/site-packages/xarray/core/common.py:306, in AttrAccessMixin.__getattr__(self, name)
304 with suppress(KeyError):
305 return source[name]
--> 306 raise AttributeError(
307 f"{type(self).__name__!r} object has no attribute {name!r}"
308 )
AttributeError: 'DataTree' object has no attribute 'subset'
How can we select multiple groups in a DataTree, like we can in Dataset?
Note: for now, the workaround I have been using is to always convert the DataTree to Dataset before selecting multiple variables: print(xdt.to_dataset()[["a"]])
It only works to select variables, not groups: xds = xr.Dataset({"a": xr.DataArray([0], dims="x")})
xdt = xr.DataTree.from_dict({"a": xds, "b": xds, "c": xds})
print(xdt)
print(xdt.to_dataset())
The DataTree only contains groups, no varaibles, so this workaround will not work. xdt.to_dataset()[["a", "b"]] -> Fails with I would like to be able to select by paths, to select indistinctively groups or variables (eg below group print(xdt[["/b", "/c/a"]]) The closest form of subset is using xds = xr.Dataset({"a": xr.DataArray([0], dims="x")})
xdt = xr.DataTree.from_dict({"toto/tata/a": xds, "toto/tete/a": xds, "toto/tutu/a": xds})
print(xdt)
print(xdt.filter(lambda node: node.path in ["/toto/tata", "/toto/tete/a"]))
Note that the resulting selected group is devoid of variables: A more complex filter function to achieve group selection can be used, but seems a bit cumbersome, and still does not select variables: xds = xr.Dataset({"a": xr.DataArray([0], dims="x"), "b": xr.DataArray([1], dims="y")})
xdt = xr.DataTree.from_dict(
{
"/toto/tata/bar": xds,
"/toto/tete/bar": xds,
"/toto/tutu/bar": xds,
"/toto/tata/foo": xds,
"/toto/tete/foo": xds,
"/toto/tutu/foo": xds,
}
)
print(xdt)
def select(xdt: xr.DataTree, paths: list[str]) -> xr.DataTree:
return xdt.filter(lambda node: any(node.path.startswith(p) for p in paths))
sel = select(
xdt,
[
"/toto/tata", # Whole tata group selected with bar and foo
"/toto/tete/bar", # Only bar subgroup is selected
"/toto/tutu/foo/a", # Nothing selected!
],
)
print(sel)
|
What happened?
calling
dt[["a"]]
suggests to use.subset
which does not exist.What did you expect to happen?
No response
Minimal Complete Verifiable Example
MVCE confirmation
Relevant log output
Anything else we need to know?
No response
Environment
master
The text was updated successfully, but these errors were encountered: