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

Fix DataArrayRolling.__iter__ with center=True #6744

Merged
merged 7 commits into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ Bug fixes
- :py:meth:`open_dataset` with dask and ``~`` in the path now resolves the home directory
instead of raising an error. (:issue:`6707`, :pull:`6710`)
By `Michael Niklas <https://github.com/headtr1ck>`_.
- :py:meth:`DataArrayRolling.__iter__` with ``center=True`` now works correctly.
(:issue:`6739`, :pull:`6744`)
By `Michael Niklas <https://github.com/headtr1ck>`_.

Documentation
~~~~~~~~~~~~~
Expand Down
9 changes: 6 additions & 3 deletions xarray/core/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,15 @@ def __init__(
# TODO legacy attribute
self.window_labels = self.obj[self.dim[0]]

def __iter__(self) -> Iterator[tuple[RollingKey, DataArray]]:
def __iter__(self) -> Iterator[tuple[DataArray, DataArray]]:
if self.ndim > 1:
raise ValueError("__iter__ is only supported for 1d-rolling")
stops = np.arange(1, len(self.window_labels) + 1)

offset = int(self.window[0] + 1) // 2 if self.center[0] else 1
stops = np.arange(offset, self.obj.sizes[self.dim[0]] + offset)
starts = stops - int(self.window[0])
starts[: int(self.window[0])] = 0
starts[: int(self.window[0]) - offset] = 0

headtr1ck marked this conversation as resolved.
Show resolved Hide resolved
for (label, start, stop) in zip(self.window_labels, starts, stops):
window = self.obj.isel({self.dim[0]: slice(start, stop)})

Expand Down
Loading