Skip to content

Commit

Permalink
ndrolling fixes (pydata#4329)
Browse files Browse the repository at this point in the history
* ndrolling repr fix

* better tests

* correct doc and error message.

* also update the mean dimensions.
  • Loading branch information
fujiisoup authored Aug 9, 2020
1 parent f02ca53 commit df7b2ea
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
6 changes: 3 additions & 3 deletions doc/computation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
8 changes: 5 additions & 3 deletions xarray/core/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down
10 changes: 10 additions & 0 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit df7b2ea

Please sign in to comment.