Skip to content

BUG: Fix for rolling with uneven nanosecond windows have wrong results #43998

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 4 commits into from
Oct 15, 2021
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 @@ -506,6 +506,7 @@ Groupby/resample/rolling
- 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`)
- Fixed bug in :meth:`Series.rolling` and :meth:`DataFrame.rolling` for centered datetimelike windows with uneven nanosecond (:issue:`43997`)
- Bug in :meth:`GroupBy.nth` failing on ``axis=1`` (:issue:`43926`)
- Fixed bug in :meth:`Series.rolling` and :meth:`DataFrame.rolling` not respecting right bound on centered datetime-like windows, if the index contain duplicates (:issue:`#3944`)

Expand Down
8 changes: 8 additions & 0 deletions pandas/_libs/window/indexers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ def calculate_variable_window_bounds(
if closed in ['left', 'both']:
left_closed = True

# GH 43997:
# If the forward and the backward facing windows
# would result in a fraction of 1/2 a nanosecond
# we need to make both interval ends inclusive.
if center and window_size % 2 == 1:
right_closed = True
left_closed = True

if index[num_values - 1] < index[0]:
index_growth_sign = -1

Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/window/test_rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -1276,6 +1276,23 @@ def test_rolling_decreasing_indices_centered(window, closed, expected, frame_or_
tm.assert_equal(result_dec, expected_dec)


@pytest.mark.parametrize(
"window,expected",
[
("1ns", [1.0, 1.0, 1.0, 1.0]),
("3ns", [2.0, 3.0, 3.0, 2.0]),
],
)
def test_rolling_center_nanosecond_resolution(
window, closed, expected, frame_or_series
):
index = date_range("2020", periods=4, freq="1ns")
df = frame_or_series([1, 1, 1, 1], index=index, dtype=float)
expected = frame_or_series(expected, index=index, dtype=float)
result = df.rolling(window, closed=closed, center=True).sum()
tm.assert_equal(result, expected)


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