Skip to content

Commit 2aa3ffe

Browse files
rosagoldpalmb
andauthored
[FIX] rolling to respect duplicate datetime indices on the right bound of centered windows (#43945)
* [FIX] rolling now respect duplicate datetime indices on the right bound of centered windows. * blackified * added whatsnew entry Co-authored-by: Bert Palm <[email protected]>
1 parent b80f8ef commit 2aa3ffe

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@ Groupby/resample/rolling
506506
- 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`)
507507
- Bug in :meth:`GroupBy.mean` failing with ``complex`` dtype (:issue:`43701`)
508508
- 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`)
509+
- 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`)
509510

510511
Reshaping
511512
^^^^^^^^^

pandas/_libs/window/indexers.pyx

-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ def calculate_variable_window_bounds(
122122
elif ((index[j] - end_bound) * index_growth_sign == 0 and
123123
right_closed):
124124
end[i] = j + 1
125-
break
126125
elif (index[j] - end_bound) * index_growth_sign >= 0:
127126
end[i] = j
128127
break

pandas/tests/window/test_rolling.py

+33
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,39 @@ def test_datetimelike_centered_offset_covers_all(
250250
tm.assert_equal(result, expected)
251251

252252

253+
@pytest.mark.parametrize(
254+
"window,closed,expected",
255+
[
256+
("2D", "right", [4, 4, 4, 4, 4, 4, 2, 2]),
257+
("2D", "left", [2, 2, 4, 4, 4, 4, 4, 4]),
258+
("2D", "both", [4, 4, 6, 6, 6, 6, 4, 4]),
259+
("2D", "neither", [2, 2, 2, 2, 2, 2, 2, 2]),
260+
],
261+
)
262+
def test_datetimelike_nonunique_index_centering(
263+
window, closed, expected, frame_or_series
264+
):
265+
index = DatetimeIndex(
266+
[
267+
"2020-01-01",
268+
"2020-01-01",
269+
"2020-01-02",
270+
"2020-01-02",
271+
"2020-01-03",
272+
"2020-01-03",
273+
"2020-01-04",
274+
"2020-01-04",
275+
]
276+
)
277+
278+
df = frame_or_series([1] * 8, index=index, dtype=float)
279+
expected = frame_or_series(expected, index=index, dtype=float)
280+
281+
result = df.rolling(window, center=True, closed=closed).sum()
282+
283+
tm.assert_equal(result, expected)
284+
285+
253286
def test_even_number_window_alignment():
254287
# see discussion in GH 38780
255288
s = Series(range(3), index=date_range(start="2020-01-01", freq="D", periods=3))

0 commit comments

Comments
 (0)