Skip to content

Commit 217c19a

Browse files
mroeschkejreback
authored andcommitted
BUG: Rolling.__iter__ includes on index columns in the result (pandas-dev#41405)
Co-authored-by: Jeff Reback <[email protected]>
1 parent 1d77d25 commit 217c19a

File tree

3 files changed

+6
-2
lines changed

3 files changed

+6
-2
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,7 @@ Groupby/resample/rolling
10381038
- Bug in :meth:`DataFrameGroupBy.__getitem__` with non-unique columns incorrectly returning a malformed :class:`SeriesGroupBy` instead of :class:`DataFrameGroupBy` (:issue:`41427`)
10391039
- Bug in :meth:`DataFrameGroupBy.transform` with non-unique columns incorrectly raising ``AttributeError`` (:issue:`41427`)
10401040
- Bug in :meth:`Resampler.apply` with non-unique columns incorrectly dropping duplicated columns (:issue:`41445`)
1041+
- Bug in :meth:`DataFrame.rolling.__iter__` where ``on`` was not assigned to the index of the resulting objects (:issue:`40373`)
10411042
- Bug in :meth:`DataFrameGroupBy.transform` and :meth:`DataFrameGroupBy.agg` with ``engine="numba"`` where ``*args`` were being cached with the user passed function (:issue:`41647`)
10421043

10431044
Reshaping

pandas/core/window/rolling.py

+1
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ def __repr__(self) -> str:
291291

292292
def __iter__(self):
293293
obj = self._create_data(self._selected_obj)
294+
obj = obj.set_axis(self._on)
294295
indexer = self._get_window_indexer()
295296

296297
start, end = indexer.get_window_bounds(

pandas/tests/window/test_rolling.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ def test_iter_rolling_dataframe(df, expected, window, min_periods):
742742
],
743743
)
744744
def test_iter_rolling_on_dataframe(expected, window):
745-
# GH 11704
745+
# GH 11704, 40373
746746
df = DataFrame(
747747
{
748748
"A": [1, 2, 3, 4, 5],
@@ -751,7 +751,9 @@ def test_iter_rolling_on_dataframe(expected, window):
751751
}
752752
)
753753

754-
expected = [DataFrame(values, index=index) for (values, index) in expected]
754+
expected = [
755+
DataFrame(values, index=df.loc[index, "C"]) for (values, index) in expected
756+
]
755757
for (expected, actual) in zip(expected, df.rolling(window, on="C")):
756758
tm.assert_frame_equal(actual, expected)
757759

0 commit comments

Comments
 (0)