From 65381f3744bd00a76dbec0933971e16a37cfef21 Mon Sep 17 00:00:00 2001 From: keisuke fujii Date: Sun, 9 Aug 2020 08:32:53 +0900 Subject: [PATCH 1/4] ndrolling repr fix --- xarray/core/rolling.py | 4 ++-- xarray/tests/test_dataarray.py | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/xarray/core/rolling.py b/xarray/core/rolling.py index 5f996565243..62d14fadc01 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) diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 7ccf1eb14bc..7bd2fcf1e0b 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -6178,6 +6178,8 @@ def test_rolling_iter(da): actual.values[actual.values.nonzero()], expected.values[expected.values.nonzero()], ) + # no error + repr(rolling_obj) def test_rolling_doc(da): From 7e1fbf87b898851d26de3eb3ab328637d61437bc Mon Sep 17 00:00:00 2001 From: keisuke fujii Date: Sun, 9 Aug 2020 08:48:03 +0900 Subject: [PATCH 2/4] better tests --- xarray/tests/test_dataarray.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 7bd2fcf1e0b..75b0b4847b3 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -6178,8 +6178,16 @@ def test_rolling_iter(da): actual.values[actual.values.nonzero()], expected.values[expected.values.nonzero()], ) - # no error - repr(rolling_obj) + + +@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): From 61c9ff9d4bbbe53c03c6b467e1dafabc3660144e Mon Sep 17 00:00:00 2001 From: keisuke fujii Date: Sun, 9 Aug 2020 19:49:12 +0900 Subject: [PATCH 3/4] correct doc and error message. --- doc/computation.rst | 4 ++-- xarray/core/rolling.py | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/doc/computation.rst b/doc/computation.rst index 474c3905981..b096099385e 100644 --- a/doc/computation.rst +++ b/doc/computation.rst @@ -234,7 +234,7 @@ 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) @@ -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 62d14fadc01..fb38c0c7fe6 100644 --- a/xarray/core/rolling.py +++ b/xarray/core/rolling.py @@ -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): From 3b9cf9819679a9080a26ba469b78563981a3a9d1 Mon Sep 17 00:00:00 2001 From: keisuke fujii Date: Sun, 9 Aug 2020 20:26:06 +0900 Subject: [PATCH 4/4] also update the mean dimensions. --- doc/computation.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/computation.rst b/doc/computation.rst index b096099385e..dcfe270a942 100644 --- a/doc/computation.rst +++ b/doc/computation.rst @@ -236,7 +236,7 @@ windowed rolling, convolution, short-time FFT etc. # rolling with 2-point stride 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.