Skip to content

Commit 6470a1a

Browse files
committed
BUG: Respect axis when doing DataFrame.expanding
Closes gh-23372.
1 parent 360e727 commit 6470a1a

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

doc/source/whatsnew/v0.24.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,7 @@ Groupby/Resample/Rolling
12131213
- Bug in :meth:`SeriesGroupBy.mean` when values were integral but could not fit inside of int64, overflowing instead. (:issue:`22487`)
12141214
- :func:`RollingGroupby.agg` and :func:`ExpandingGroupby.agg` now support multiple aggregation functions as parameters (:issue:`15072`)
12151215
- Bug in :meth:`DataFrame.resample` and :meth:`Series.resample` when resampling by a weekly offset (``'W'``) across a DST transition (:issue:`9119`, :issue:`21459`)
1216+
- Bug in :meth:`DataFrame.expanding` in which the ``axis`` argument was not being respected during aggregations (:issue:`23372`)
12161217

12171218
Reshaping
12181219
^^^^^^^^^

pandas/core/window.py

+9
Original file line numberDiff line numberDiff line change
@@ -1867,6 +1867,15 @@ def _constructor(self):
18671867

18681868
def _get_window(self, other=None):
18691869
obj = self._selected_obj
1870+
axis = self.obj._get_axis_number(self.axis)
1871+
1872+
# If axis == 0, leave `obj` alone.
1873+
# If axis == 1, transpose.
1874+
#
1875+
# Other values of axis are invalid.
1876+
if axis == 1:
1877+
obj = obj.T
1878+
18701879
if other is None:
18711880
return (max(len(obj), self.min_periods) if self.min_periods
18721881
else len(obj))

pandas/tests/test_window.py

+10
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,16 @@ def test_iter_raises(self, klass):
714714
with pytest.raises(NotImplementedError):
715715
iter(obj.expanding(2))
716716

717+
def test_expanding_axis(self):
718+
# see gh-23372.
719+
df = DataFrame(np.ones((10, 20)))
720+
721+
exp_row = [np.nan] * 2 + [float(i) for i in range(3, 21)]
722+
expected = DataFrame([exp_row] * 10)
723+
724+
result = df.expanding(3, axis=1).sum()
725+
tm.assert_frame_equal(result, expected)
726+
717727

718728
class TestEWM(Base):
719729

0 commit comments

Comments
 (0)