Skip to content

Commit c152288

Browse files
authored
DEPR: Enforce rolling.count min_periods deprecation (#48839)
1 parent d2acdee commit c152288

File tree

7 files changed

+15
-39
lines changed

7 files changed

+15
-39
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ Removal of prior version deprecations/changes
147147
- Disallow passing non-round floats to :class:`Timestamp` with ``unit="M"`` or ``unit="Y"`` (:issue:`47266`)
148148
- Removed :func:`is_extension_type` in favor of :func:`is_extension_array_dtype` (:issue:`29457`)
149149
- Remove :meth:`DataFrameGroupBy.pad` and :meth:`DataFrameGroupBy.backfill` (:issue:`45076`)
150+
- Enforced :meth:`Rolling.count` with ``min_periods=None`` to default to the size of the window (:issue:`31302`)
150151

151152
.. ---------------------------------------------------------------------------
152153
.. _whatsnew_200.performance:

pandas/core/window/rolling.py

+7-22
Original file line numberDiff line numberDiff line change
@@ -1936,21 +1936,21 @@ def aggregate(self, func, *args, **kwargs):
19361936
"""
19371937
>>> s = pd.Series([2, 3, np.nan, 10])
19381938
>>> s.rolling(2).count()
1939-
0 1.0
1939+
0 NaN
19401940
1 2.0
19411941
2 1.0
19421942
3 1.0
19431943
dtype: float64
19441944
>>> s.rolling(3).count()
1945-
0 1.0
1946-
1 2.0
1945+
0 NaN
1946+
1 NaN
19471947
2 2.0
19481948
3 2.0
19491949
dtype: float64
19501950
>>> s.rolling(4).count()
1951-
0 1.0
1952-
1 2.0
1953-
2 2.0
1951+
0 NaN
1952+
1 NaN
1953+
2 NaN
19541954
3 3.0
19551955
dtype: float64
19561956
"""
@@ -1960,22 +1960,7 @@ def aggregate(self, func, *args, **kwargs):
19601960
agg_method="count",
19611961
)
19621962
def count(self, numeric_only: bool = False):
1963-
if self.min_periods is None:
1964-
warnings.warn(
1965-
(
1966-
"min_periods=None will default to the size of window "
1967-
"consistent with other methods in a future version. "
1968-
"Specify min_periods=0 instead."
1969-
),
1970-
FutureWarning,
1971-
stacklevel=find_stack_level(),
1972-
)
1973-
self.min_periods = 0
1974-
result = super().count()
1975-
self.min_periods = None
1976-
else:
1977-
result = super().count(numeric_only)
1978-
return result
1963+
return super().count(numeric_only)
19791964

19801965
@doc(
19811966
template_header,

pandas/tests/window/test_base_indexer.py

-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ def get_window_bounds(self, num_values, min_periods, center, closed, step):
147147
),
148148
],
149149
)
150-
@pytest.mark.filterwarnings("ignore:min_periods:FutureWarning")
151150
def test_rolling_forward_window(
152151
frame_or_series, func, np_func, expected, np_kwargs, step
153152
):

pandas/tests/window/test_groupby.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,7 @@ def test_getitem_multiple(self, roll_frame):
9696
"mean",
9797
"min",
9898
"max",
99-
pytest.param(
100-
"count",
101-
marks=pytest.mark.filterwarnings("ignore:min_periods:FutureWarning"),
102-
),
99+
"count",
103100
"kurt",
104101
"skew",
105102
],

pandas/tests/window/test_rolling.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -738,8 +738,7 @@ def test_rolling_count_default_min_periods_with_null_values(frame_or_series):
738738
expected_counts = [1.0, 2.0, 3.0, 2.0, 2.0, 2.0, 3.0]
739739

740740
# GH 31302
741-
with tm.assert_produces_warning(FutureWarning):
742-
result = frame_or_series(values).rolling(3).count()
741+
result = frame_or_series(values).rolling(3, min_periods=0).count()
743742
expected = frame_or_series(expected_counts)
744743
tm.assert_equal(result, expected)
745744

pandas/tests/window/test_rolling_functions.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,11 @@
2323
[
2424
[np.mean, "mean", {}],
2525
[np.nansum, "sum", {}],
26-
pytest.param(
26+
[
2727
lambda x: np.isfinite(x).astype(float).sum(),
2828
"count",
2929
{},
30-
marks=pytest.mark.filterwarnings("ignore:min_periods:FutureWarning"),
31-
),
30+
],
3231
[np.median, "median", {}],
3332
[np.min, "min", {}],
3433
[np.max, "max", {}],
@@ -50,12 +49,11 @@ def test_series(series, compare_func, roll_func, kwargs, step):
5049
[
5150
[np.mean, "mean", {}],
5251
[np.nansum, "sum", {}],
53-
pytest.param(
52+
[
5453
lambda x: np.isfinite(x).astype(float).sum(),
5554
"count",
5655
{},
57-
marks=pytest.mark.filterwarnings("ignore:min_periods:FutureWarning"),
58-
),
56+
],
5957
[np.median, "median", {}],
6058
[np.min, "min", {}],
6159
[np.max, "max", {}],

pandas/tests/window/test_timeseries_window.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -592,10 +592,7 @@ def test_freqs_ops(self, freq, op, result_data):
592592
[
593593
"sum",
594594
"mean",
595-
pytest.param(
596-
"count",
597-
marks=pytest.mark.filterwarnings("ignore:min_periods:FutureWarning"),
598-
),
595+
"count",
599596
"median",
600597
"std",
601598
"var",

0 commit comments

Comments
 (0)