Skip to content

Commit

Permalink
BUG: rolling not calculating window bounds correctly with offset and …
Browse files Browse the repository at this point in the history
…desc dates (#40006)
  • Loading branch information
phofl committed Feb 24, 2021
1 parent 0b5c12d commit 84a9a65
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.0.rst
Expand Up @@ -438,6 +438,7 @@ Groupby/resample/rolling
- Bug in :meth:`core.window.rolling.RollingGroupby.corr` and :meth:`core.window.expanding.ExpandingGroupby.corr` where the groupby column would return 0 instead of ``np.nan`` when providing ``other`` that was longer than each group (:issue:`39591`)
- Bug in :meth:`core.window.expanding.ExpandingGroupby.corr` and :meth:`core.window.expanding.ExpandingGroupby.cov` where 1 would be returned instead of ``np.nan`` when providing ``other`` that was longer than each group (:issue:`39591`)
- Bug in :meth:`.GroupBy.mean`, :meth:`.GroupBy.median` and :meth:`DataFrame.pivot_table` not propagating metadata (:issue:`28283`)
- Bug in :meth:`Series.rolling` and :meth:`DataFrame.rolling` not calculating window bounds correctly when window is an offset and dates are in descending order (:issue:`40002`)
-

Reshaping
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/window/indexers.pyx
Expand Up @@ -88,7 +88,7 @@ def calculate_variable_window_bounds(

# left endpoint is closed
if left_closed:
start_bound -= 1
start_bound -= 1 * index_growth_sign

# advance the start bound until we are
# within the constraint
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/window/test_rolling.py
Expand Up @@ -1133,3 +1133,18 @@ def test_rolling_skew_kurt_large_value_range(method, values):
def test_invalid_method():
with pytest.raises(ValueError, match="method must be 'table' or 'single"):
Series(range(1)).rolling(1, method="foo")


@pytest.mark.parametrize("window", [1, "1d"])
def test_rolling_descending_date_order_with_offset(window, frame_or_series):
# GH#40002
idx = date_range(start="2020-01-01", end="2020-01-03", freq="1d")
obj = frame_or_series(range(1, 4), index=idx)
result = obj.rolling("1d", closed="left").sum()
expected = frame_or_series([np.nan, 1, 2], index=idx)
tm.assert_equal(result, expected)

result = obj.iloc[::-1].rolling("1d", closed="left").sum()
idx = date_range(start="2020-01-03", end="2020-01-01", freq="-1d")
expected = frame_or_series([np.nan, 3, 2], index=idx)
tm.assert_equal(result, expected)

0 comments on commit 84a9a65

Please sign in to comment.