Skip to content

Commit 66f6c17

Browse files
mathausedcherian
andauthored
DataTree: sel & isel add error context (#10154)
* DataTree: sel & isel add error context * add test * changelog --------- Co-authored-by: Deepak Cherian <[email protected]>
1 parent 7ffdcc7 commit 66f6c17

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

doc/whats-new.rst

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ Bug fixes
4343
This fixes the issue where using the ``zarr_version`` parameter would raise a deprecation warning telling the user to use
4444
a non-existent ``zarr_format`` parameter instead. (:issue:`10163`, :pull:`10164`)
4545
By `Karl Krauth <https://github.com/Karl-Krauth>`_.
46+
- :py:meth:`DataTree.sel` and :py:meth:`DataTree.isel` display the path of the first failed node again (:pull:`10154`).
47+
By `Mathias Hauser <https://github.com/mathause>`_.
4648
- Fix grouped and resampled ``first``, ``last`` with datetimes (:issue:`10169`, :pull:`10173`)
4749
By `Deepak Cherian <https://github.com/dcherian>`_.
4850

xarray/core/datatree.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from xarray.core.dataset import Dataset
3333
from xarray.core.dataset_variables import DataVariables
3434
from xarray.core.datatree_mapping import (
35+
_handle_errors_with_path_context,
3536
map_over_datasets,
3637
)
3738
from xarray.core.formatting import (
@@ -1845,7 +1846,8 @@ def _selective_indexing(
18451846
result = {}
18461847
for path, node in self.subtree_with_keys:
18471848
node_indexers = {k: v for k, v in indexers.items() if k in node.dims}
1848-
node_result = func(node.dataset, node_indexers)
1849+
func_with_error_context = _handle_errors_with_path_context(path)(func)
1850+
node_result = func_with_error_context(node.dataset, node_indexers)
18491851
# Indexing datasets corresponding to each node results in redundant
18501852
# coordinates when indexes from a parent node are inherited.
18511853
# Ideally, we would avoid creating such coordinates in the first

xarray/tests/test_datatree.py

+20
Original file line numberDiff line numberDiff line change
@@ -1955,6 +1955,26 @@ def test_sel(self) -> None:
19551955
)
19561956
assert_identical(actual, expected)
19571957

1958+
def test_sel_isel_error_has_node_info(self) -> None:
1959+
tree = DataTree.from_dict(
1960+
{
1961+
"/first": xr.Dataset({"a": ("x", [1, 2, 3])}, coords={"x": [1, 2, 3]}),
1962+
"/second": xr.Dataset({"b": ("x", [4, 5])}, coords={"x": [2, 3]}),
1963+
}
1964+
)
1965+
1966+
with pytest.raises(
1967+
KeyError,
1968+
match="Raised whilst mapping function over node with path 'second'",
1969+
):
1970+
tree.sel(x=1)
1971+
1972+
with pytest.raises(
1973+
IndexError,
1974+
match="Raised whilst mapping function over node with path 'first'",
1975+
):
1976+
tree.isel(x=4)
1977+
19581978

19591979
class TestAggregations:
19601980
def test_reduce_method(self) -> None:

0 commit comments

Comments
 (0)