Skip to content

BUG: Series rolling count ignores min_periods #30923

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
398bf7d
BUG: Series rolling count ignores min_periods (GH26996)
fujiaxiang Jan 11, 2020
5d471c9
Merge remote-tracking branch 'upstream/master' into series_rolling_co…
fujiaxiang Jan 17, 2020
47f4e61
ENH: handles min_periods argument in rolling.count (GH26996)
fujiaxiang Jan 17, 2020
df2a3e9
DOC: moved whatsnew to V1.1.0
fujiaxiang Jan 18, 2020
554abfa
Merge remote-tracking branch 'upstream/master' into series_rolling_co…
fujiaxiang Jan 18, 2020
c4878b0
Merge remote-tracking branch 'upstream/master' into series_rolling_co…
fujiaxiang Jan 18, 2020
b0f5baa
Merge remote-tracking branch 'upstream/master' into series_rolling_co…
fujiaxiang Jan 19, 2020
bfe10f0
TST: added more tests (GH26996)
fujiaxiang Jan 21, 2020
1a90629
Merge branch 'master' into series_rolling_count_ignores_min_periods
fujiaxiang Jan 21, 2020
be0926b
Merge remote-tracking branch 'upstream/master' into series_rolling_co…
fujiaxiang Jan 21, 2020
c0046b6
BUG: updated rolling and expanding count for consistency (GH26996)
fujiaxiang Jan 21, 2020
8807ba1
CLN: further cleaned code (GH26996)
fujiaxiang Jan 22, 2020
7aa1582
Merge remote-tracking branch 'upstream/master' into series_rolling_co…
fujiaxiang Jan 23, 2020
bcce129
TST: cleaned up tests (GH26996)
fujiaxiang Jan 24, 2020
1a4352b
BUG: changed min_periods default to 0 for rolling and expanding (GH26…
fujiaxiang Jan 24, 2020
9fa4531
BUG: reverted non-relevant changes accidentally added (GH26996)
fujiaxiang Jan 24, 2020
987c033
BUG: small change in whatsnew (GH26996)
fujiaxiang Jan 25, 2020
a00532a
CLN: slight cleanup (GH26996)
fujiaxiang Jan 25, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,7 @@ Groupby/resample/rolling
- Bug in :meth:`DataFrame.groupby` when using nunique on axis=1 (:issue:`30253`)
- Bug in :meth:`GroupBy.quantile` with multiple list-like q value and integer column names (:issue:`30289`)
- Bug in :meth:`GroupBy.pct_change` and :meth:`core.groupby.SeriesGroupBy.pct_change` causes ``TypeError`` when ``fill_method`` is ``None`` (:issue:`30463`)
- Bug in :meth:`Rolling.count` and :meth:`Expanding.count` argument ``min_periods`` ignored (:issue:`26996`)

Reshaping
^^^^^^^^^
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -1185,14 +1185,16 @@ def count(self):

window = self._get_window()
window = min(window, len(obj)) if not self.center else window
min_periods = self.min_periods if self.min_periods is not None else 0
min_periods = min(min_periods, len(obj)) if not self.center else min_periods

results = []
for b in blocks:
result = b.notna().astype(int)
result = self._constructor(
result,
window=window,
min_periods=0,
min_periods=min_periods,
center=self.center,
axis=self.axis,
closed=self.closed,
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/window/test_expanding.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,10 @@ def test_expanding_axis(self, axis_frame):

result = df.expanding(3, axis=axis_frame).sum()
tm.assert_frame_equal(result, expected)


def test_expanding_count_with_min_periods():
# GH 26996
result = Series(range(5)).expanding(min_periods=3).count()
expected = Series([np.nan, np.nan, 3.0, 4.0, 5.0])
tm.assert_series_equal(result, expected)
7 changes: 7 additions & 0 deletions pandas/tests/window/test_rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,3 +426,10 @@ def test_min_periods1():
result = df["a"].rolling(3, center=True, min_periods=1).max()
expected = pd.Series([1.0, 2.0, 2.0, 2.0, 1.0], name="a")
tm.assert_series_equal(result, expected)


def test_rolling_count_with_min_periods():
# GH 26996
result = Series(range(5)).rolling(3, min_periods=3).count()
expected = Series([np.nan, np.nan, 3.0, 3.0, 3.0])
tm.assert_series_equal(result, expected)