Skip to content

Commit aa3ff0b

Browse files
mroeschkesimonjayhawkins
authored andcommitted
Backport PR pandas-dev#39604: REGR: Rolling.count setting min_periods after call
1 parent ee8c1ff commit aa3ff0b

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

doc/source/whatsnew/v1.2.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Fixed regressions
2121
- Fixed regression in :meth:`~DataFrame.to_pickle` failing to create bz2/xz compressed pickle files with ``protocol=5`` (:issue:`39002`)
2222
- Fixed regression in :func:`pandas.testing.assert_series_equal` and :func:`pandas.testing.assert_frame_equal` always raising ``AssertionError`` when comparing extension dtypes (:issue:`39410`)
2323
- Fixed regression in :meth:`~DataFrame.to_csv` opening ``codecs.StreamWriter`` in binary mode instead of in text mode and ignoring user-provided ``mode`` (:issue:`39247`)
24+
- Fixed regression in :meth:`core.window.rolling.Rolling.count` where the ``min_periods`` argument would be set to ``0`` after the operation (:issue:`39554`)
2425
-
2526

2627
.. ---------------------------------------------------------------------------

pandas/core/window/rolling.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -2016,7 +2016,11 @@ def count(self):
20162016
FutureWarning,
20172017
)
20182018
self.min_periods = 0
2019-
return super().count()
2019+
result = super().count()
2020+
self.min_periods = None
2021+
else:
2022+
result = super().count()
2023+
return result
20202024

20212025
@Substitution(name="rolling")
20222026
@Appender(_shared_docs["apply"])

pandas/tests/window/test_api.py

+14
Original file line numberDiff line numberDiff line change
@@ -319,3 +319,17 @@ def test_multiple_agg_funcs(func, window_size, expected_vals):
319319
result = window.agg({"low": ["mean", "max"], "high": ["mean", "min"]})
320320

321321
tm.assert_frame_equal(result, expected)
322+
323+
324+
@pytest.mark.filterwarnings("ignore:min_periods:FutureWarning")
325+
def test_dont_modify_attributes_after_methods(
326+
arithmetic_win_operators, closed, center, min_periods
327+
):
328+
# GH 39554
329+
roll_obj = Series(range(1)).rolling(
330+
1, center=center, closed=closed, min_periods=min_periods
331+
)
332+
expected = {attr: getattr(roll_obj, attr) for attr in roll_obj._attributes}
333+
getattr(roll_obj, arithmetic_win_operators)()
334+
result = {attr: getattr(roll_obj, attr) for attr in roll_obj._attributes}
335+
assert result == expected

pandas/tests/window/test_base_indexer.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,10 @@ def test_rolling_forward_window(constructor, func, np_func, expected, np_kwargs)
170170

171171
# Check that the function output matches applying an alternative function
172172
# if min_periods isn't specified
173-
rolling3 = constructor(values).rolling(window=indexer)
173+
# GH 39604: After count-min_periods deprecation, apply(lambda x: len(x))
174+
# is equivalent to count after setting min_periods=0
175+
min_periods = 0 if func == "count" else None
176+
rolling3 = constructor(values).rolling(window=indexer, min_periods=min_periods)
174177
result3 = getattr(rolling3, func)()
175178
expected3 = constructor(rolling3.apply(lambda x: np_func(x, **np_kwargs)))
176179
tm.assert_equal(result3, expected3)

0 commit comments

Comments
 (0)