Skip to content

Commit

Permalink
Fix rolling(min_periods=) with int and null data with mode.pandas_compat
Browse files Browse the repository at this point in the history
  • Loading branch information
mroeschke committed Jan 24, 2025
1 parent 00dca76 commit 3d315d8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
7 changes: 5 additions & 2 deletions python/cudf/cudf/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

if TYPE_CHECKING:
from cudf.core.column.column import ColumnBase
from cudf.core.indexed_frame import IndexedFrame


class _RollingBase:
Expand Down Expand Up @@ -205,7 +206,7 @@ class Rolling(GetAttrGetItemMixin, _RollingBase, Reducible):

def __init__(
self,
obj,
obj: IndexedFrame,
window,
min_periods=None,
center: bool = False,
Expand All @@ -216,7 +217,9 @@ def __init__(
step: int | None = None,
method: str = "single",
):
self.obj = obj
if cudf.get_option("mode.pandas_compatible"):
obj = obj.nans_to_nulls()
self.obj = obj # type: ignore[assignment]
self.window = window
self.min_periods = min_periods
self.center = center
Expand Down
11 changes: 10 additions & 1 deletion python/cudf/cudf/tests/test_rolling.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2021-2024, NVIDIA CORPORATION.
# Copyright (c) 2021-2025, NVIDIA CORPORATION.

import math

Expand Down Expand Up @@ -517,3 +517,12 @@ def test_rolling_series():
actual = df.groupby("b")["a"].rolling(5).mean()

assert_eq(expected, actual)


@pytest.mark.parametrize("klass", ["DataFrame", "Series"])
def test_pandas_compat_int_nan_min_periods(klass):
data = [None, 1, 2, None, 4, 6, 11]
with cudf.option_context("mode.pandas_compatible", True):
result = getattr(cudf, klass)(data).rolling(2, min_periods=1).sum()
expected = getattr(pd, klass)(data).rolling(2, min_periods=1).sum()
assert_eq(result, expected)

0 comments on commit 3d315d8

Please sign in to comment.