Skip to content
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 @@ -502,6 +502,7 @@ Groupby/resample/rolling
- Bug in :meth:`DataFrame.groupby.rolling` when specifying ``on`` and calling ``__getitem__`` would subsequently return incorrect results (:issue:`43355`)
- 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`)

Reshaping
^^^^^^^^^
Expand Down
6 changes: 4 additions & 2 deletions pandas/_libs/window/indexers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,11 @@ def calculate_variable_window_bounds(
if center:
end_bound = index[0] + index_growth_sign * window_size / 2
for j in range(0, num_values):
if (index[j] < end_bound) or (index[j] == end_bound and right_closed):
if (index[j] - end_bound) * index_growth_sign < 0:
end[0] = j + 1
elif index[j] >= end_bound:
elif (index[j] - end_bound) * index_growth_sign == 0 and right_closed:
end[0] = j + 1
elif (index[j] - end_bound) * index_growth_sign >= 0:
end[0] = 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 @@ -1210,6 +1210,39 @@ def test_rolling_decreasing_indices(method):
assert np.abs(decreasing.values[::-1][:-4] - increasing.values[4:]).max() < 1e-12


@pytest.mark.parametrize(
"window,closed,expected",
[
("2s", "right", [1.0, 3.0, 5.0, 3.0]),
("2s", "left", [0.0, 1.0, 3.0, 5.0]),
("2s", "both", [1.0, 3.0, 6.0, 5.0]),
("2s", "neither", [0.0, 1.0, 2.0, 3.0]),
("3s", "right", [1.0, 3.0, 6.0, 5.0]),
("3s", "left", [1.0, 3.0, 6.0, 5.0]),
("3s", "both", [1.0, 3.0, 6.0, 5.0]),
("3s", "neither", [1.0, 3.0, 6.0, 5.0]),
],
)
def test_rolling_decreasing_indices_centered(window, closed, expected, frame_or_series):
"""
Ensure that a symmetrical inverted index return same result as non-inverted.
"""
# GH 43927

index = date_range("2020", periods=4, freq="1s")
df_inc = frame_or_series(range(4), index=index)
df_dec = frame_or_series(range(4), index=index[::-1])

expected_inc = frame_or_series(expected, index=index)
expected_dec = frame_or_series(expected, index=index[::-1])

result_inc = df_inc.rolling(window, closed=closed, center=True).sum()
result_dec = df_dec.rolling(window, closed=closed, center=True).sum()

tm.assert_equal(result_inc, expected_inc)
tm.assert_equal(result_dec, expected_dec)


@pytest.mark.parametrize(
"method,expected",
[
Expand Down