Skip to content

Commit 96d8f68

Browse files
committed
Show list of dimensions in error messages of Rolling and Coarsen, update tests
1 parent 1b8b444 commit 96d8f68

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

xarray/core/rolling.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ def __init__(
102102
self.center = self._mapping_to_list(center, default=False)
103103
self.obj: T_Xarray = obj
104104

105+
missing_dims = tuple(dim for dim in self.dim if dim not in self.obj.dims)
106+
if missing_dims:
107+
# NOTE: we raise KeyError here but ValueError in Coarsen.
108+
raise KeyError(
109+
f"Window dimensions {missing_dims} not found in {self.obj.__class__.__name__} "
110+
f"dimensions {tuple(self.obj.dims)}"
111+
)
112+
105113
# attributes
106114
if min_periods is not None and min_periods <= 0:
107115
raise ValueError("min_periods must be greater than zero or None")
@@ -624,8 +632,7 @@ def __init__(
624632
xarray.DataArray.groupby
625633
"""
626634
super().__init__(obj, windows, min_periods, center)
627-
if any(d not in self.obj.dims for d in self.dim):
628-
raise KeyError(self.dim)
635+
629636
# Keep each Rolling object as a dictionary
630637
self.rollings = {}
631638
for key, da in self.obj.data_vars.items():
@@ -839,10 +846,11 @@ def __init__(
839846
self.side = side
840847
self.boundary = boundary
841848

842-
absent_dims = [dim for dim in windows.keys() if dim not in self.obj.dims]
843-
if absent_dims:
849+
missing_dims = tuple(dim for dim in windows.keys() if dim not in self.obj.dims)
850+
if missing_dims:
844851
raise ValueError(
845-
f"Dimensions {absent_dims!r} not found in {self.obj.__class__.__name__}."
852+
f"Window dimensions {missing_dims} not found in {self.obj.__class__.__name__} "
853+
f"dimensions {tuple(self.obj.dims)}"
846854
)
847855
if not utils.is_dict_like(coord_func):
848856
coord_func = {d: coord_func for d in self.obj.dims} # type: ignore[misc]

xarray/tests/test_coarsen.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717

1818

1919
def test_coarsen_absent_dims_error(ds: Dataset) -> None:
20-
with pytest.raises(ValueError, match=r"not found in Dataset."):
20+
with pytest.raises(
21+
ValueError,
22+
match=r"Window dimensions \('foo',\) not found in Dataset dimensions",
23+
):
2124
ds.coarsen(foo=2)
2225

2326

xarray/tests/test_rolling.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ def test_rolling_properties(self, da) -> None:
7777
with pytest.raises(ValueError, match="min_periods must be greater than zero"):
7878
da.rolling(time=2, min_periods=0)
7979

80+
with pytest.raises(
81+
KeyError,
82+
match=r"\('foo',\) not found in DataArray dimensions",
83+
):
84+
da.rolling(foo=2)
85+
8086
@pytest.mark.parametrize("name", ("sum", "mean", "std", "min", "max", "median"))
8187
@pytest.mark.parametrize("center", (True, False, None))
8288
@pytest.mark.parametrize("min_periods", (1, None))
@@ -540,6 +546,11 @@ def test_rolling_properties(self, ds) -> None:
540546
ds.rolling(time=2, min_periods=0)
541547
with pytest.raises(KeyError, match="time2"):
542548
ds.rolling(time2=2)
549+
with pytest.raises(
550+
KeyError,
551+
match=r"\('foo',\) not found in Dataset dimensions",
552+
):
553+
ds.rolling(foo=2)
543554

544555
@pytest.mark.parametrize(
545556
"name", ("sum", "mean", "std", "var", "min", "max", "median")

0 commit comments

Comments
 (0)