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

BUG: Ensure rolling groupby doesn't segfault with center=True #35562

Merged
merged 4 commits into from
Aug 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.1.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Fixed regressions
~~~~~~~~~~~~~~~~~

- Fixed regression where :func:`read_csv` would raise a ``ValueError`` when ``pandas.options.mode.use_inf_as_na`` was set to ``True`` (:issue:`35493`).
-
- Fixed regression in :class:`pandas.core.groupby.RollingGroupby` where a segfault would occur with ``center=True`` and an odd number of values (:issue:`35552`)
Copy link
Member

@jorisvandenbossche jorisvandenbossche Aug 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pandas.core.groupby.RollingGroupby doesn't exist, so this link won't work (it's in the window module). But, we actually don't have this in the API docs it seems, so won't work anyway. I would just do something like .groupby(..).rolling(..) to indicate which operation is affected.

-

.. ---------------------------------------------------------------------------
Expand Down
6 changes: 6 additions & 0 deletions pandas/core/window/indexers.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,4 +319,10 @@ def get_window_bounds(
end_arrays.append(window_indicies.take(end))
start = np.concatenate(start_arrays)
end = np.concatenate(end_arrays)
# GH 35552: Need to adjust start and end based on the nans appended to values
# when center=True
if num_values > len(start):
offset = num_values - len(start)
start = np.concatenate([start, np.array([end[-1]] * offset)])
end = np.concatenate([end, np.array([end[-1]] * offset)])
return start, end
65 changes: 65 additions & 0 deletions pandas/tests/window/test_grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,68 @@ def foo(x):
name="value",
)
tm.assert_series_equal(result, expected)

def test_groupby_rolling_center_center(self):
# GH 35552
series = Series(range(1, 6))
result = series.groupby(series).rolling(center=True, window=3).mean()
expected = Series(
[np.nan] * 5,
index=pd.MultiIndex.from_tuples(((1, 0), (2, 1), (3, 2), (4, 3), (5, 4))),
)
tm.assert_series_equal(result, expected)

series = Series(range(1, 5))
result = series.groupby(series).rolling(center=True, window=3).mean()
expected = Series(
[np.nan] * 4,
index=pd.MultiIndex.from_tuples(((1, 0), (2, 1), (3, 2), (4, 3))),
)
tm.assert_series_equal(result, expected)

df = pd.DataFrame({"a": ["a"] * 5 + ["b"] * 6, "b": range(11)})
result = df.groupby("a").rolling(center=True, window=3).mean()
expected = pd.DataFrame(
[np.nan, 1, 2, 3, np.nan, np.nan, 6, 7, 8, 9, np.nan],
index=pd.MultiIndex.from_tuples(
(
("a", 0),
("a", 1),
("a", 2),
("a", 3),
("a", 4),
("b", 5),
("b", 6),
("b", 7),
("b", 8),
("b", 9),
("b", 10),
),
names=["a", None],
),
columns=["b"],
)
tm.assert_frame_equal(result, expected)

df = pd.DataFrame({"a": ["a"] * 5 + ["b"] * 5, "b": range(10)})
result = df.groupby("a").rolling(center=True, window=3).mean()
expected = pd.DataFrame(
[np.nan, 1, 2, 3, np.nan, np.nan, 6, 7, 8, np.nan],
index=pd.MultiIndex.from_tuples(
(
("a", 0),
("a", 1),
("a", 2),
("a", 3),
("a", 4),
("b", 5),
("b", 6),
("b", 7),
("b", 8),
("b", 9),
),
names=["a", None],
),
columns=["b"],
)
tm.assert_frame_equal(result, expected)