From 4a51f374041a83a04f7539f0432c21cf0caad66d Mon Sep 17 00:00:00 2001 From: Jordan Murphy <35613487+jordan-d-murphy@users.noreply.github.com> Date: Sat, 10 Feb 2024 01:10:35 -0700 Subject: [PATCH 1/6] DOC: fix PR02 errors in docstring for pandas.core.groupby.DataFrameGroupBy.rolling and pandas.core.groupby.SeriesGroupBy.rolling --- ci/code_checks.sh | 2 -- pandas/core/groupby/groupby.py | 20 +++++++++++++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/ci/code_checks.sh b/ci/code_checks.sh index 9c9cd2cc6650e..f48a9e11dc9e1 100755 --- a/ci/code_checks.sh +++ b/ci/code_checks.sh @@ -137,9 +137,7 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then pandas.Interval\ pandas.Grouper\ pandas.core.groupby.DataFrameGroupBy.nth\ - pandas.core.groupby.DataFrameGroupBy.rolling\ pandas.core.groupby.SeriesGroupBy.nth\ - pandas.core.groupby.SeriesGroupBy.rolling\ pandas.core.groupby.DataFrameGroupBy.plot\ pandas.core.groupby.SeriesGroupBy.plot # There should be no backslash in the final line, please keep this comment in the last ignored function RET=$(($RET + $?)) ; echo $MSG "DONE" diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index c10ea9636cba5..aac5a59568911 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -3628,7 +3628,16 @@ def resample(self, rule, *args, include_groups: bool = True, **kwargs) -> Resamp ) @final - def rolling(self, *args, **kwargs) -> RollingGroupby: + def rolling( + self, + window: int, + min_periods: int, + center: bool, + win_type: str, + on: str, + closed: str, + method: str, + ) -> RollingGroupby: """ Return a rolling grouper, providing rolling functionality per group. @@ -3758,10 +3767,15 @@ def rolling(self, *args, **kwargs) -> RollingGroupby: return RollingGroupby( self._selected_obj, - *args, + window=window, + min_periods=min_periods, + center=center, + win_type=win_type, + on=on, + closed=closed, + method=method, _grouper=self._grouper, _as_index=self.as_index, - **kwargs, ) @final From aa3998afbb93254d939af4cae452b2076327c830 Mon Sep 17 00:00:00 2001 From: Jordan Murphy <35613487+jordan-d-murphy@users.noreply.github.com> Date: Sat, 10 Feb 2024 02:11:17 -0700 Subject: [PATCH 2/6] added default values to rolling() parameters --- pandas/core/groupby/groupby.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index aac5a59568911..2900c0f926348 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -47,6 +47,7 @@ class providing the base-class of operations. DtypeObj, FillnaOptions, IndexLabel, + IntervalClosedType, NDFrameT, PositionalIndexer, RandomState, @@ -139,6 +140,7 @@ class providing the base-class of operations. ) if TYPE_CHECKING: + from pandas._libs.tslibs import BaseOffset from pandas._typing import ( Any, Concatenate, @@ -147,6 +149,7 @@ class providing the base-class of operations. T, ) + from pandas.core.indexers.objects import BaseIndexer from pandas.core.resample import Resampler from pandas.core.window import ( ExpandingGroupby, @@ -3630,13 +3633,13 @@ def resample(self, rule, *args, include_groups: bool = True, **kwargs) -> Resamp @final def rolling( self, - window: int, - min_periods: int, - center: bool, - win_type: str, - on: str, - closed: str, - method: str, + window: int | datetime.timedelta | str | BaseOffset | BaseIndexer, + min_periods: int | None = None, + center: bool = False, + win_type: str | None = None, + on: str | None = None, + closed: IntervalClosedType | None = None, + method: str = "single", ) -> RollingGroupby: """ Return a rolling grouper, providing rolling functionality per group. From 15099221369fb1b68b9136edd45a0fb7cd64342a Mon Sep 17 00:00:00 2001 From: Jordan Murphy <35613487+jordan-d-murphy@users.noreply.github.com> Date: Sat, 10 Feb 2024 02:57:55 -0700 Subject: [PATCH 3/6] Update test case test_rolling_wrong_param_min_period --- pandas/tests/groupby/test_groupby.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index db45067162bc3..298c526517432 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -2423,7 +2423,7 @@ def test_rolling_wrong_param_min_period(): test_df = DataFrame([name_l, val_l]).T test_df.columns = ["name", "val"] - result_error_msg = r"__init__\(\) got an unexpected keyword argument 'min_period'" + result_error_msg = r"GroupBy.rolling() got unexpected keyword argument 'min_period'" with pytest.raises(TypeError, match=result_error_msg): test_df.groupby("name")["val"].rolling(window=2, min_period=1).sum() From b7fc295931bc23ca97b4621c4a72caa6322be2a7 Mon Sep 17 00:00:00 2001 From: Jordan Murphy <35613487+jordan-d-murphy@users.noreply.github.com> Date: Sat, 10 Feb 2024 03:21:36 -0700 Subject: [PATCH 4/6] Updated test case wording --- pandas/tests/groupby/test_groupby.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index 298c526517432..bd7822ea38128 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -2423,8 +2423,10 @@ def test_rolling_wrong_param_min_period(): test_df = DataFrame([name_l, val_l]).T test_df.columns = ["name", "val"] - result_error_msg = r"GroupBy.rolling() got unexpected keyword argument 'min_period'" - with pytest.raises(TypeError, match=result_error_msg): + result_err_msg = ( + r"GroupBy.rolling() got an unexpected keyword argument 'min_period'" + ) + with pytest.raises(TypeError, match=result_err_msg): test_df.groupby("name")["val"].rolling(window=2, min_period=1).sum() From 0e89e46559f45ccf220578d6ddecf44da54081cd Mon Sep 17 00:00:00 2001 From: Jordan Murphy <35613487+jordan-d-murphy@users.noreply.github.com> Date: Sat, 10 Feb 2024 03:42:22 -0700 Subject: [PATCH 5/6] add escape sequence to regex --- pandas/tests/groupby/test_groupby.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index bd7822ea38128..3eebef32c90c7 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -2424,7 +2424,7 @@ def test_rolling_wrong_param_min_period(): test_df.columns = ["name", "val"] result_err_msg = ( - r"GroupBy.rolling() got an unexpected keyword argument 'min_period'" + r"GroupBy.rolling\(\) got an unexpected keyword argument 'min_period'" ) with pytest.raises(TypeError, match=result_err_msg): test_df.groupby("name")["val"].rolling(window=2, min_period=1).sum() From 94a1d57927a7c4fcd6e5e028921e492e8cfe6905 Mon Sep 17 00:00:00 2001 From: Jordan Murphy <35613487+jordan-d-murphy@users.noreply.github.com> Date: Sat, 10 Feb 2024 12:58:57 -0700 Subject: [PATCH 6/6] Updating regex to accept multiple origin functions --- pandas/tests/groupby/test_groupby.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index 3eebef32c90c7..2d06c2f258539 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -2423,10 +2423,10 @@ def test_rolling_wrong_param_min_period(): test_df = DataFrame([name_l, val_l]).T test_df.columns = ["name", "val"] - result_err_msg = ( - r"GroupBy.rolling\(\) got an unexpected keyword argument 'min_period'" + result_error_msg = ( + r"^[a-zA-Z._]*\(\) got an unexpected keyword argument 'min_period'$" ) - with pytest.raises(TypeError, match=result_err_msg): + with pytest.raises(TypeError, match=result_error_msg): test_df.groupby("name")["val"].rolling(window=2, min_period=1).sum()