diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index accfeee484430..823aab53624a1 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -387,6 +387,7 @@ Groupby/Resample/Rolling - Ensured that ordering of outputs in ``groupby`` aggregation functions is consistent across all versions of Python (:issue:`25692`) - Ensured that result group order is correct when grouping on an ordered ``Categorical`` and specifying ``observed=True`` (:issue:`25871`, :issue:`25167`) - Bug in :meth:`pandas.core.window.Rolling.min` and :meth:`pandas.core.window.Rolling.max` that caused a memory leak (:issue:`25893`) +- Bug in :meth:`pandas.core.window.Rolling.count` and `pandas.core.window.Expanding.count` was previously ignoring the axis keyword (:issue:`13503`) - Bug in :meth:`pandas.core.groupby.GroupBy.idxmax` and :meth:`pandas.core.groupby.GroupBy.idxmin` with datetime column would return incorrect dtype (:issue:`25444`, :issue:`15306`) - Bug in :meth:`pandas.core.groupby.GroupBy.cumsum`, :meth:`pandas.core.groupby.GroupBy.cumprod`, :meth:`pandas.core.groupby.GroupBy.cummin` and :meth:`pandas.core.groupby.GroupBy.cummax` with categorical column having absent categories, would return incorrect result or segfault (:issue:`16771`) diff --git a/pandas/core/window.py b/pandas/core/window.py index eb65ca7a92584..7c7025498537d 100644 --- a/pandas/core/window.py +++ b/pandas/core/window.py @@ -938,6 +938,7 @@ def count(self): result = b.notna().astype(int) result = self._constructor(result, window=window, min_periods=0, center=self.center, + axis=self.axis, closed=self.closed).sum() results.append(result) diff --git a/pandas/tests/test_window.py b/pandas/tests/test_window.py index 937b3218eb5c6..37798141105f8 100644 --- a/pandas/tests/test_window.py +++ b/pandas/tests/test_window.py @@ -648,7 +648,7 @@ def test_iter_raises(self, klass): with pytest.raises(NotImplementedError): iter(obj.rolling(2)) - def test_rolling_axis(self, axis_frame): + def test_rolling_axis_sum(self, axis_frame): # see gh-23372. df = DataFrame(np.ones((10, 20))) axis = df._get_axis_number(axis_frame) @@ -667,6 +667,20 @@ def test_rolling_axis(self, axis_frame): result = df.rolling(3, axis=axis_frame).sum() tm.assert_frame_equal(result, expected) + def test_rolling_axis_count(self, axis_frame): + # see gh-26055 + df = DataFrame({'x': range(3), 'y': range(3)}) + + axis = df._get_axis_number(axis_frame) + + if axis in [0, 'index']: + expected = DataFrame({'x': [1.0, 2.0, 2.0], 'y': [1.0, 2.0, 2.0]}) + else: + expected = DataFrame({'x': [1.0, 1.0, 1.0], 'y': [2.0, 2.0, 2.0]}) + + result = df.rolling(2, axis=axis_frame).count() + tm.assert_frame_equal(result, expected) + class TestExpanding(Base):