Skip to content

BUG: Incorrect variable window bounds for first row when offset cover all rows #42776

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 14 commits into from
Aug 7, 2021
Merged
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Fixed regressions
Bug fixes
~~~~~~~~~
- 1D slices over extension types turn into N-dimensional slices over ExtensionArrays (:issue:`42430`)
- Fixed bug in :meth:`Series.rolling` and :meth:`DataFrame.rolling` not calculating window bounds correctly for the first row when window is an offset that covers all the rows (:issue:`42753`)
-

.. ---------------------------------------------------------------------------
Expand Down
8 changes: 4 additions & 4 deletions pandas/_libs/window/indexers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ def calculate_variable_window_bounds(
else:
end[0] = 0
if center:
for j in range(0, num_values + 1):
if (index[j] == index[0] + index_growth_sign * window_size / 2 and
right_closed):
for j in range(0, num_values):
if ((index[j] < index[0] + index_growth_sign * window_size / 2) or
(index[j] == index[0] + index_growth_sign * window_size / 2 and
right_closed)):
end[0] = j + 1
break
elif index[j] >= index[0] + index_growth_sign * window_size / 2:
end[0] = j
break
Expand Down
30 changes: 30 additions & 0 deletions pandas/tests/window/test_rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,36 @@ def test_datetimelike_centered_selections(
tm.assert_frame_equal(result, expected, check_dtype=False)


@pytest.mark.parametrize(
"window,closed,expected",
[
("3s", "right", [3.0, 3.0, 3.0]),
("3s", "both", [3.0, 3.0, 3.0]),
("3s", "left", [3.0, 3.0, 3.0]),
("3s", "neither", [3.0, 3.0, 3.0]),
("2s", "right", [3.0, 2.0, 2.0]),
("2s", "both", [3.0, 3.0, 3.0]),
("2s", "left", [1.0, 3.0, 3.0]),
("2s", "neither", [1.0, 2.0, 2.0]),
],
)
def test_datetimelike_centered_offset_covers_all(
window, closed, expected, frame_or_series
):
# GH 42753

index = [
Timestamp("20130101 09:00:01"),
Timestamp("20130101 09:00:02"),
Timestamp("20130101 09:00:02"),
]
df = frame_or_series([1, 1, 1], index=index)

result = df.rolling(window, closed=closed, center=True).sum()
expected = frame_or_series(expected, index=index)
tm.assert_equal(result, expected, check_dtype=False)


def test_even_number_window_alignment():
# see discussion in GH 38780
s = Series(range(3), index=date_range(start="2020-01-01", freq="D", periods=3))
Expand Down