Skip to content

Commit 84a9a65

Browse files
authored
BUG: rolling not calculating window bounds correctly with offset and desc dates (pandas-dev#40006)
1 parent 0b5c12d commit 84a9a65

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ Groupby/resample/rolling
438438
- 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`)
439439
- 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`)
440440
- Bug in :meth:`.GroupBy.mean`, :meth:`.GroupBy.median` and :meth:`DataFrame.pivot_table` not propagating metadata (:issue:`28283`)
441+
- 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`)
441442
-
442443

443444
Reshaping

pandas/_libs/window/indexers.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def calculate_variable_window_bounds(
8888

8989
# left endpoint is closed
9090
if left_closed:
91-
start_bound -= 1
91+
start_bound -= 1 * index_growth_sign
9292

9393
# advance the start bound until we are
9494
# within the constraint

pandas/tests/window/test_rolling.py

+15
Original file line numberDiff line numberDiff line change
@@ -1133,3 +1133,18 @@ def test_rolling_skew_kurt_large_value_range(method, values):
11331133
def test_invalid_method():
11341134
with pytest.raises(ValueError, match="method must be 'table' or 'single"):
11351135
Series(range(1)).rolling(1, method="foo")
1136+
1137+
1138+
@pytest.mark.parametrize("window", [1, "1d"])
1139+
def test_rolling_descending_date_order_with_offset(window, frame_or_series):
1140+
# GH#40002
1141+
idx = date_range(start="2020-01-01", end="2020-01-03", freq="1d")
1142+
obj = frame_or_series(range(1, 4), index=idx)
1143+
result = obj.rolling("1d", closed="left").sum()
1144+
expected = frame_or_series([np.nan, 1, 2], index=idx)
1145+
tm.assert_equal(result, expected)
1146+
1147+
result = obj.iloc[::-1].rolling("1d", closed="left").sum()
1148+
idx = date_range(start="2020-01-03", end="2020-01-01", freq="-1d")
1149+
expected = frame_or_series([np.nan, 3, 2], index=idx)
1150+
tm.assert_equal(result, expected)

0 commit comments

Comments
 (0)