Skip to content

fixed rolling for a decreasing index, added a test for that #43928

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 9, 2021
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
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
16 changes: 16 additions & 0 deletions pandas/tests/window/test_rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -1210,6 +1210,22 @@ def test_rolling_decreasing_indices(method):
assert np.abs(decreasing.values[::-1][:-4] - increasing.values[4:]).max() < 1e-12


@pytest.mark.parametrize("window", ["0s", "2s", "3s"])
def test_rolling_decreasing_indices_invariant(window, center, closed):
"""
Ensure that a symmetrical inverted index return same result as non-inverted.
"""

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

result = df_dec.rolling(window, closed=closed, center=center).sum()
expected = df_inc.rolling(window, closed=closed, center=center).sum()

tm.assert_equal(result.values, expected.values)


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