From 83a1a7f1edd03b715bf978ab1eaf9a02d018a61d Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Wed, 28 Sep 2022 11:35:15 -0700 Subject: [PATCH 1/4] DEPR: Enforce rolling.count min_periods --- pandas/core/window/rolling.py | 17 +---------------- pandas/tests/window/test_rolling.py | 3 +-- pandas/tests/window/test_rolling_functions.py | 10 ++++------ 3 files changed, 6 insertions(+), 24 deletions(-) diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index 3225b9c0d2174..a30aafe6f128e 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -1960,22 +1960,7 @@ def aggregate(self, func, *args, **kwargs): agg_method="count", ) def count(self, numeric_only: bool = False): - if self.min_periods is None: - warnings.warn( - ( - "min_periods=None will default to the size of window " - "consistent with other methods in a future version. " - "Specify min_periods=0 instead." - ), - FutureWarning, - stacklevel=find_stack_level(inspect.currentframe()), - ) - self.min_periods = 0 - result = super().count() - self.min_periods = None - else: - result = super().count(numeric_only) - return result + return super().count(numeric_only) @doc( template_header, diff --git a/pandas/tests/window/test_rolling.py b/pandas/tests/window/test_rolling.py index c9ec2985488be..9203ce9366b6a 100644 --- a/pandas/tests/window/test_rolling.py +++ b/pandas/tests/window/test_rolling.py @@ -738,8 +738,7 @@ def test_rolling_count_default_min_periods_with_null_values(frame_or_series): expected_counts = [1.0, 2.0, 3.0, 2.0, 2.0, 2.0, 3.0] # GH 31302 - with tm.assert_produces_warning(FutureWarning): - result = frame_or_series(values).rolling(3).count() + result = frame_or_series(values).rolling(3).count() expected = frame_or_series(expected_counts) tm.assert_equal(result, expected) diff --git a/pandas/tests/window/test_rolling_functions.py b/pandas/tests/window/test_rolling_functions.py index 9ab4ff13796d6..fc64c8efc8fcf 100644 --- a/pandas/tests/window/test_rolling_functions.py +++ b/pandas/tests/window/test_rolling_functions.py @@ -23,12 +23,11 @@ [ [np.mean, "mean", {}], [np.nansum, "sum", {}], - pytest.param( + [ lambda x: np.isfinite(x).astype(float).sum(), "count", {}, - marks=pytest.mark.filterwarnings("ignore:min_periods:FutureWarning"), - ), + ], [np.median, "median", {}], [np.min, "min", {}], [np.max, "max", {}], @@ -50,12 +49,11 @@ def test_series(series, compare_func, roll_func, kwargs, step): [ [np.mean, "mean", {}], [np.nansum, "sum", {}], - pytest.param( + [ lambda x: np.isfinite(x).astype(float).sum(), "count", {}, - marks=pytest.mark.filterwarnings("ignore:min_periods:FutureWarning"), - ), + ], [np.median, "median", {}], [np.min, "min", {}], [np.max, "max", {}], From b1d1a9bc186398633788072a58ab8032662839dd Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Wed, 28 Sep 2022 17:22:49 -0700 Subject: [PATCH 2/4] Fix test, remove unneeded filters --- pandas/tests/window/test_base_indexer.py | 1 - pandas/tests/window/test_groupby.py | 5 +---- pandas/tests/window/test_rolling.py | 2 +- pandas/tests/window/test_timeseries_window.py | 5 +---- 4 files changed, 3 insertions(+), 10 deletions(-) diff --git a/pandas/tests/window/test_base_indexer.py b/pandas/tests/window/test_base_indexer.py index eb5278c2b33ea..372db414f195b 100644 --- a/pandas/tests/window/test_base_indexer.py +++ b/pandas/tests/window/test_base_indexer.py @@ -148,7 +148,6 @@ def get_window_bounds(self, num_values, min_periods, center, closed, step): ), ], ) -@pytest.mark.filterwarnings("ignore:min_periods:FutureWarning") def test_rolling_forward_window(constructor, func, np_func, expected, np_kwargs, step): # GH 32865 values = np.arange(10.0) diff --git a/pandas/tests/window/test_groupby.py b/pandas/tests/window/test_groupby.py index 5f4805eaa01d2..5ca2b791e4427 100644 --- a/pandas/tests/window/test_groupby.py +++ b/pandas/tests/window/test_groupby.py @@ -96,10 +96,7 @@ def test_getitem_multiple(self, roll_frame): "mean", "min", "max", - pytest.param( - "count", - marks=pytest.mark.filterwarnings("ignore:min_periods:FutureWarning"), - ), + "count", "kurt", "skew", ], diff --git a/pandas/tests/window/test_rolling.py b/pandas/tests/window/test_rolling.py index 9203ce9366b6a..e4d96e270fa66 100644 --- a/pandas/tests/window/test_rolling.py +++ b/pandas/tests/window/test_rolling.py @@ -738,7 +738,7 @@ def test_rolling_count_default_min_periods_with_null_values(frame_or_series): expected_counts = [1.0, 2.0, 3.0, 2.0, 2.0, 2.0, 3.0] # GH 31302 - result = frame_or_series(values).rolling(3).count() + result = frame_or_series(values).rolling(3, min_periods=0).count() expected = frame_or_series(expected_counts) tm.assert_equal(result, expected) diff --git a/pandas/tests/window/test_timeseries_window.py b/pandas/tests/window/test_timeseries_window.py index 907c654570273..eaa4181ac5df6 100644 --- a/pandas/tests/window/test_timeseries_window.py +++ b/pandas/tests/window/test_timeseries_window.py @@ -592,10 +592,7 @@ def test_freqs_ops(self, freq, op, result_data): [ "sum", "mean", - pytest.param( - "count", - marks=pytest.mark.filterwarnings("ignore:min_periods:FutureWarning"), - ), + "count", "median", "std", "var", From 3c37c0bf2bb41d79f579e11ae743ea4b18f13ab6 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Thu, 29 Sep 2022 14:05:56 -0700 Subject: [PATCH 3/4] Fix docstring --- pandas/core/window/rolling.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index a30aafe6f128e..8c695a56b80f0 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -1936,21 +1936,21 @@ def aggregate(self, func, *args, **kwargs): """ >>> s = pd.Series([2, 3, np.nan, 10]) >>> s.rolling(2).count() - 0 1.0 + 0 NaN 1 2.0 2 1.0 3 1.0 dtype: float64 >>> s.rolling(3).count() - 0 1.0 - 1 2.0 + 0 NaN + 1 NaN 2 2.0 3 2.0 dtype: float64 >>> s.rolling(4).count() - 0 1.0 - 1 2.0 - 2 2.0 + 0 NaN + 1 NaN + 2 NaN 3 3.0 dtype: float64 """ From 0eb36e206cd652cd1f9627cd6274b73f7e82cacb Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Wed, 19 Oct 2022 11:03:39 -0700 Subject: [PATCH 4/4] add whatsnew --- doc/source/whatsnew/v2.0.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 596c16a5b3621..728b59ae6b0c2 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -146,6 +146,7 @@ Removal of prior version deprecations/changes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - Disallow passing non-round floats to :class:`Timestamp` with ``unit="M"`` or ``unit="Y"`` (:issue:`47266`) - Remove :meth:`DataFrameGroupBy.pad` and :meth:`DataFrameGroupBy.backfill` (:issue:`45076`) +- Enforced :meth:`Rolling.count` with ``min_periods=None`` to default to the size of the window (:issue:`31302`) .. --------------------------------------------------------------------------- .. _whatsnew_200.performance: