Skip to content

BUG: Rolling.__iter__ includes on index columns in the result #41405

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

Merged
merged 5 commits into from
May 26, 2021
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,7 @@ Groupby/resample/rolling
- Bug in :meth:`DataFrameGroupBy.__getitem__` with non-unique columns incorrectly returning a malformed :class:`SeriesGroupBy` instead of :class:`DataFrameGroupBy` (:issue:`41427`)
- Bug in :meth:`DataFrameGroupBy.transform` with non-unique columns incorrectly raising ``AttributeError`` (:issue:`41427`)
- Bug in :meth:`Resampler.apply` with non-unique columns incorrectly dropping duplicated columns (:issue:`41445`)
- Bug in :meth:`DataFrame.rolling.__iter__` where ``on`` was not assigned to the index of the resulting objects (:issue:`40373`)
- Bug in :meth:`DataFrameGroupBy.transform` and :meth:`DataFrameGroupBy.agg` with ``engine="numba"`` where ``*args`` were being cached with the user passed function (:issue:`41647`)

Reshaping
Expand Down
1 change: 1 addition & 0 deletions pandas/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ def __repr__(self) -> str:

def __iter__(self):
obj = self._create_data(self._selected_obj)
obj = obj.set_axis(self._on)
indexer = self._get_window_indexer()

start, end = indexer.get_window_bounds(
Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/window/test_rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ def test_iter_rolling_dataframe(df, expected, window, min_periods):
],
)
def test_iter_rolling_on_dataframe(expected, window):
# GH 11704
# GH 11704, 40373
df = DataFrame(
{
"A": [1, 2, 3, 4, 5],
Expand All @@ -751,7 +751,9 @@ def test_iter_rolling_on_dataframe(expected, window):
}
)

expected = [DataFrame(values, index=index) for (values, index) in expected]
expected = [
DataFrame(values, index=df.loc[index, "C"]) for (values, index) in expected
]
for (expected, actual) in zip(expected, df.rolling(window, on="C")):
tm.assert_frame_equal(actual, expected)

Expand Down