Skip to content

[FIX] rolling to respect duplicate datetime indices on the right bound of centered windows #43945

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 3 commits into from
Oct 13, 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.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ Groupby/resample/rolling
- Bug in :meth:`GroupBy.apply` with time-based :class:`Grouper` objects incorrectly raising ``ValueError`` in corner cases where the grouping vector contains a ``NaT`` (:issue:`43500`, :issue:`43515`)
- Bug in :meth:`GroupBy.mean` failing with ``complex`` dtype (:issue:`43701`)
- Fixed bug in :meth:`Series.rolling` and :meth:`DataFrame.rolling` not calculating window bounds correctly for the first row when ``center=True`` and index is decreasing (:issue:`43927`)
- Fixed bug in :meth:`Series.rolling` and :meth:`DataFrame.rolling` not respecting right bound on centered datetime-like windows, if the index contain duplicates (:issue:`#3944`)

Reshaping
^^^^^^^^^
Expand Down
1 change: 0 additions & 1 deletion pandas/_libs/window/indexers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ def calculate_variable_window_bounds(
elif ((index[j] - end_bound) * index_growth_sign == 0 and
right_closed):
end[i] = j + 1
break
elif (index[j] - end_bound) * index_growth_sign >= 0:
end[i] = j
break
Expand Down
33 changes: 33 additions & 0 deletions pandas/tests/window/test_rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,39 @@ def test_datetimelike_centered_offset_covers_all(
tm.assert_equal(result, expected)


@pytest.mark.parametrize(
"window,closed,expected",
[
("2D", "right", [4, 4, 4, 4, 4, 4, 2, 2]),
("2D", "left", [2, 2, 4, 4, 4, 4, 4, 4]),
("2D", "both", [4, 4, 6, 6, 6, 6, 4, 4]),
("2D", "neither", [2, 2, 2, 2, 2, 2, 2, 2]),
],
)
def test_datetimelike_nonunique_index_centering(
window, closed, expected, frame_or_series
):
index = DatetimeIndex(
[
"2020-01-01",
"2020-01-01",
"2020-01-02",
"2020-01-02",
"2020-01-03",
"2020-01-03",
"2020-01-04",
"2020-01-04",
]
)

df = frame_or_series([1] * 8, index=index, dtype=float)
expected = frame_or_series(expected, index=index, dtype=float)

result = df.rolling(window, center=True, closed=closed).sum()

tm.assert_equal(result, expected)


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