Skip to content

Commit 6a2f7b9

Browse files
dicristinafeefladder
authored andcommitted
BUG: Incorrect variable window bounds for first row when offset cover all rows (pandas-dev#42776)
1 parent 04f8a44 commit 6a2f7b9

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

doc/source/whatsnew/v1.3.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Bug fixes
3232
~~~~~~~~~
3333
- Bug in :meth:`pandas.read_excel` modifies the dtypes dictionary when reading a file with duplicate columns (:issue:`42462`)
3434
- 1D slices over extension types turn into N-dimensional slices over ExtensionArrays (:issue:`42430`)
35+
- Fixed bug in :meth:`Series.rolling` and :meth:`DataFrame.rolling` not calculating window bounds correctly for the first row when ``center=True`` and ``window`` is an offset that covers all the rows (:issue:`42753`)
3536
- :meth:`.Styler.hide_columns` now hides the index name header row as well as column headers (:issue:`42101`)
3637
- :meth:`.Styler.set_sticky` has amended CSS to control the column/index names and ensure the correct sticky positions (:issue:`42537`)
3738
- Bug in de-serializing datetime indexes in PYTHONOPTIMIZED mode (:issue:`42866`)

pandas/_libs/window/indexers.pyx

+4-5
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,11 @@ def calculate_variable_window_bounds(
7979
else:
8080
end[0] = 0
8181
if center:
82-
for j in range(0, num_values + 1):
83-
if (index[j] == index[0] + index_growth_sign * window_size / 2 and
84-
right_closed):
82+
end_bound = index[0] + index_growth_sign * window_size / 2
83+
for j in range(0, num_values):
84+
if (index[j] < end_bound) or (index[j] == end_bound and right_closed):
8585
end[0] = j + 1
86-
break
87-
elif index[j] >= index[0] + index_growth_sign * window_size / 2:
86+
elif index[j] >= end_bound:
8887
end[0] = j
8988
break
9089

pandas/tests/window/test_rolling.py

+30
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,36 @@ def test_datetimelike_centered_selections(
220220
tm.assert_frame_equal(result, expected, check_dtype=False)
221221

222222

223+
@pytest.mark.parametrize(
224+
"window,closed,expected",
225+
[
226+
("3s", "right", [3.0, 3.0, 3.0]),
227+
("3s", "both", [3.0, 3.0, 3.0]),
228+
("3s", "left", [3.0, 3.0, 3.0]),
229+
("3s", "neither", [3.0, 3.0, 3.0]),
230+
("2s", "right", [3.0, 2.0, 2.0]),
231+
("2s", "both", [3.0, 3.0, 3.0]),
232+
("2s", "left", [1.0, 3.0, 3.0]),
233+
("2s", "neither", [1.0, 2.0, 2.0]),
234+
],
235+
)
236+
def test_datetimelike_centered_offset_covers_all(
237+
window, closed, expected, frame_or_series
238+
):
239+
# GH 42753
240+
241+
index = [
242+
Timestamp("20130101 09:00:01"),
243+
Timestamp("20130101 09:00:02"),
244+
Timestamp("20130101 09:00:02"),
245+
]
246+
df = frame_or_series([1, 1, 1], index=index)
247+
248+
result = df.rolling(window, closed=closed, center=True).sum()
249+
expected = frame_or_series(expected, index=index)
250+
tm.assert_equal(result, expected)
251+
252+
223253
def test_even_number_window_alignment():
224254
# see discussion in GH 38780
225255
s = Series(range(3), index=date_range(start="2020-01-01", freq="D", periods=3))

0 commit comments

Comments
 (0)