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: diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index 2abca7d182859..80b782d582561 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 """ @@ -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(), - ) - 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_base_indexer.py b/pandas/tests/window/test_base_indexer.py index 83149b7599a22..fc4a20e4bb568 100644 --- a/pandas/tests/window/test_base_indexer.py +++ b/pandas/tests/window/test_base_indexer.py @@ -147,7 +147,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( frame_or_series, func, np_func, expected, np_kwargs, step ): diff --git a/pandas/tests/window/test_groupby.py b/pandas/tests/window/test_groupby.py index c01bb519875b9..7064d465bb555 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 c9ec2985488be..e4d96e270fa66 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, min_periods=0).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", {}], 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",