Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ndrolling repr fix #4329

Merged
merged 4 commits into from
Aug 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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