diff --git a/doc/computation.rst b/doc/computation.rst index 474c3905981..dcfe270a942 100644 --- a/doc/computation.rst +++ b/doc/computation.rst @@ -234,9 +234,9 @@ windowed rolling, convolution, short-time FFT etc. .. ipython:: python # rolling with 2-point stride - rolling_da = r.construct("window_dim", stride=2) + rolling_da = r.construct(x="x_win", y="y_win", stride=2) rolling_da - rolling_da.mean("window_dim", skipna=False) + rolling_da.mean(["x_win", "y_win"], skipna=False) Because the ``DataArray`` given by ``r.construct('window_dim')`` is a view of the original array, it is memory efficient. @@ -245,7 +245,7 @@ You can also use ``construct`` to compute a weighted rolling sum: .. ipython:: python weight = xr.DataArray([0.25, 0.5, 0.25], dims=["window"]) - arr.rolling(y=3).construct("window").dot(weight) + arr.rolling(y=3).construct(y="window").dot(weight) .. note:: numpy's Nan-aggregation functions such as ``nansum`` copy the original array. diff --git a/xarray/core/rolling.py b/xarray/core/rolling.py index 5f996565243..fb38c0c7fe6 100644 --- a/xarray/core/rolling.py +++ b/xarray/core/rolling.py @@ -99,8 +99,8 @@ def __repr__(self): """provide a nice str repr of our rolling object""" attrs = [ - "{k}->{v}".format(k=k, v=getattr(self, k)) - for k in list(self.dim) + self.window + self.center + [self.min_periods] + "{k}->{v}{c}".format(k=k, v=w, c="(center)" if c else "") + for k, w, c in zip(self.dim, self.window, self.center) ] return "{klass} [{attrs}]".format( klass=self.__class__.__name__, attrs=",".join(attrs) @@ -156,7 +156,9 @@ def _mapping_to_list( elif len(self.dim) == 1: return [arg] else: - raise ValueError("Mapping argument is necessary.") + raise ValueError( + "Mapping argument is necessary for {}d-rolling.".format(len(self.dim)) + ) class DataArrayRolling(Rolling): diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 7ccf1eb14bc..75b0b4847b3 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -6180,6 +6180,16 @@ def test_rolling_iter(da): ) +@pytest.mark.parametrize("da", (1,), indirect=True) +def test_rolling_repr(da): + rolling_obj = da.rolling(time=7) + assert repr(rolling_obj) == "DataArrayRolling [time->7]" + rolling_obj = da.rolling(time=7, center=True) + assert repr(rolling_obj) == "DataArrayRolling [time->7(center)]" + rolling_obj = da.rolling(time=7, x=3, center=True) + assert repr(rolling_obj) == "DataArrayRolling [time->7(center),x->3(center)]" + + def test_rolling_doc(da): rolling_obj = da.rolling(time=7)