Skip to content

Commit 54ceeff

Browse files
authored
REGR: RollingGroupby.corr producing incorrect results (#43854)
1 parent f9e4394 commit 54ceeff

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

doc/source/whatsnew/v1.3.4.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Fixed regressions
2424
- Fixed regression in :meth:`pandas.read_csv` raising ``UnicodeDecodeError`` exception when ``memory_map=True`` (:issue:`43540`)
2525
- Fixed regression in :meth:`DataFrame.explode` raising ``AssertionError`` when ``column`` is any scalar which is not a string (:issue:`43314`)
2626
- Fixed regression in :meth:`Series.aggregate` attempting to pass ``args`` and ``kwargs`` multiple times to the user supplied ``func`` in certain cases (:issue:`43357`)
27+
- Fixed regression when iterating over a :class:`DataFrame.groupby.rolling` object causing the resulting DataFrames to have an incorrect index if the input groupings were not sorted (:issue:`43386`)
28+
- Fixed regression in :meth:`DataFrame.groupby.rolling.cov` and :meth:`DataFrame.groupby.rolling.corr` computing incorrect results if the input groupings were not sorted (:issue:`43386`)
2729

2830
.. ---------------------------------------------------------------------------
2931

pandas/core/window/rolling.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,8 @@ def __repr__(self) -> str:
301301
return f"{type(self).__name__} [{attrs}]"
302302

303303
def __iter__(self):
304-
obj = self._create_data(self._selected_obj)
305-
obj = obj.set_axis(self._on)
304+
obj = self._selected_obj.set_axis(self._on)
305+
obj = self._create_data(obj)
306306
indexer = self._get_window_indexer()
307307

308308
start, end = indexer.get_window_bounds(
@@ -727,6 +727,7 @@ def _apply_pairwise(
727727
"""
728728
# Manually drop the grouping column first
729729
target = target.drop(columns=self._grouper.names, errors="ignore")
730+
target = self._create_data(target)
730731
result = super()._apply_pairwise(target, other, pairwise, func)
731732
# 1) Determine the levels + codes of the groupby levels
732733
if other is not None:

pandas/tests/window/test_groupby.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,42 @@ def func(x):
146146
expected = g.apply(func)
147147
tm.assert_series_equal(result, expected)
148148

149+
@pytest.mark.parametrize(
150+
"func, expected_values",
151+
[("cov", [[1.0, 1.0], [1.0, 4.0]]), ("corr", [[1.0, 0.5], [0.5, 1.0]])],
152+
)
153+
def test_rolling_corr_cov_unordered(self, func, expected_values):
154+
# GH 43386
155+
df = DataFrame(
156+
{
157+
"a": ["g1", "g2", "g1", "g1"],
158+
"b": [0, 0, 1, 2],
159+
"c": [2, 0, 6, 4],
160+
}
161+
)
162+
rol = df.groupby("a").rolling(3)
163+
result = getattr(rol, func)()
164+
expected = DataFrame(
165+
{
166+
"b": 4 * [np.nan] + expected_values[0] + 2 * [np.nan],
167+
"c": 4 * [np.nan] + expected_values[1] + 2 * [np.nan],
168+
},
169+
index=MultiIndex.from_tuples(
170+
[
171+
("g1", 0, "b"),
172+
("g1", 0, "c"),
173+
("g1", 2, "b"),
174+
("g1", 2, "c"),
175+
("g1", 3, "b"),
176+
("g1", 3, "c"),
177+
("g2", 1, "b"),
178+
("g2", 1, "c"),
179+
],
180+
names=["a", None, None],
181+
),
182+
)
183+
tm.assert_frame_equal(result, expected)
184+
149185
def test_rolling_apply(self, raw):
150186
g = self.frame.groupby("A")
151187
r = g.rolling(window=4)

pandas/tests/window/test_rolling.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,15 @@ def test_iter_rolling_on_dataframe(expected, window):
792792
tm.assert_frame_equal(actual, expected)
793793

794794

795+
def test_iter_rolling_on_dataframe_unordered():
796+
# GH 43386
797+
df = DataFrame({"a": ["x", "y", "x"], "b": [0, 1, 2]})
798+
results = list(df.groupby("a").rolling(2))
799+
expecteds = [df.iloc[idx, [1]] for idx in [[0], [0, 2], [1]]]
800+
for result, expected in zip(results, expecteds):
801+
tm.assert_frame_equal(result, expected)
802+
803+
795804
@pytest.mark.parametrize(
796805
"ser,expected,window, min_periods",
797806
[

0 commit comments

Comments
 (0)