Skip to content

Commit

Permalink
Show list of dimensions in error messages of Rolling and Coarsen, upd…
Browse files Browse the repository at this point in the history
…ate tests
  • Loading branch information
mgunyho committed Aug 19, 2023
1 parent cff01f6 commit f149ee6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
18 changes: 13 additions & 5 deletions xarray/core/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ def __init__(
self.center = self._mapping_to_list(center, default=False)
self.obj: T_Xarray = obj

missing_dims = tuple(dim for dim in self.dim if dim not in self.obj.dims)
if missing_dims:
# NOTE: we raise KeyError here but ValueError in Coarsen.
raise KeyError(
f"Window dimensions {missing_dims} not found in {self.obj.__class__.__name__} "
f"dimensions {tuple(self.obj.dims)}"
)

# attributes
if min_periods is not None and min_periods <= 0:
raise ValueError("min_periods must be greater than zero or None")
Expand Down Expand Up @@ -624,8 +632,7 @@ def __init__(
xarray.DataArray.groupby
"""
super().__init__(obj, windows, min_periods, center)
if any(d not in self.obj.dims for d in self.dim):
raise KeyError(self.dim)

# Keep each Rolling object as a dictionary
self.rollings = {}
for key, da in self.obj.data_vars.items():
Expand Down Expand Up @@ -839,10 +846,11 @@ def __init__(
self.side = side
self.boundary = boundary

absent_dims = [dim for dim in windows.keys() if dim not in self.obj.dims]
if absent_dims:
missing_dims = tuple(dim for dim in windows.keys() if dim not in self.obj.dims)
if missing_dims:
raise ValueError(
f"Dimensions {absent_dims!r} not found in {self.obj.__class__.__name__}."
f"Window dimensions {missing_dims} not found in {self.obj.__class__.__name__} "
f"dimensions {tuple(self.obj.dims)}"
)
if not utils.is_dict_like(coord_func):
coord_func = {d: coord_func for d in self.obj.dims} # type: ignore[misc]
Expand Down
5 changes: 4 additions & 1 deletion xarray/tests/test_coarsen.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@


def test_coarsen_absent_dims_error(ds: Dataset) -> None:
with pytest.raises(ValueError, match=r"not found in Dataset."):
with pytest.raises(
ValueError,
match=r"Window dimensions \('foo',\) not found in Dataset dimensions",
):
ds.coarsen(foo=2)


Expand Down
11 changes: 11 additions & 0 deletions xarray/tests/test_rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ def test_rolling_properties(self, da) -> None:
with pytest.raises(ValueError, match="min_periods must be greater than zero"):
da.rolling(time=2, min_periods=0)

with pytest.raises(
KeyError,
match=r"\('foo',\) not found in DataArray dimensions",
):
da.rolling(foo=2)

@pytest.mark.parametrize("name", ("sum", "mean", "std", "min", "max", "median"))
@pytest.mark.parametrize("center", (True, False, None))
@pytest.mark.parametrize("min_periods", (1, None))
Expand Down Expand Up @@ -540,6 +546,11 @@ def test_rolling_properties(self, ds) -> None:
ds.rolling(time=2, min_periods=0)
with pytest.raises(KeyError, match="time2"):
ds.rolling(time2=2)
with pytest.raises(
KeyError,
match=r"\('foo',\) not found in Dataset dimensions",
):
ds.rolling(foo=2)

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

0 comments on commit f149ee6

Please sign in to comment.