From 96d8f683ca99d7d4258944553f1cce7f43ec24fc Mon Sep 17 00:00:00 2001 From: mgunyho <20118130+mgunyho@users.noreply.github.com> Date: Sat, 19 Aug 2023 14:02:40 +0300 Subject: [PATCH] Show list of dimensions in error messages of Rolling and Coarsen, update tests --- xarray/core/rolling.py | 18 +++++++++++++----- xarray/tests/test_coarsen.py | 5 ++++- xarray/tests/test_rolling.py | 11 +++++++++++ 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/xarray/core/rolling.py b/xarray/core/rolling.py index 916fabe42ac..dcd01a0e0f1 100644 --- a/xarray/core/rolling.py +++ b/xarray/core/rolling.py @@ -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") @@ -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(): @@ -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] diff --git a/xarray/tests/test_coarsen.py b/xarray/tests/test_coarsen.py index d58361afdd3..e345ae691ec 100644 --- a/xarray/tests/test_coarsen.py +++ b/xarray/tests/test_coarsen.py @@ -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) diff --git a/xarray/tests/test_rolling.py b/xarray/tests/test_rolling.py index 73aebc1b1f0..0e3c0874a0a 100644 --- a/xarray/tests/test_rolling.py +++ b/xarray/tests/test_rolling.py @@ -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)) @@ -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")