Skip to content

Commit

Permalink
BUG: Respect axis when doing DataFrame.expanding (pandas-dev#23402)
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyoung authored and tm9k1 committed Nov 19, 2018
1 parent 61c07c4 commit 45dc501
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,7 @@ Groupby/Resample/Rolling
- Bug in :meth:`SeriesGroupBy.mean` when values were integral but could not fit inside of int64, overflowing instead. (:issue:`22487`)
- :func:`RollingGroupby.agg` and :func:`ExpandingGroupby.agg` now support multiple aggregation functions as parameters (:issue:`15072`)
- Bug in :meth:`DataFrame.resample` and :meth:`Series.resample` when resampling by a weekly offset (``'W'``) across a DST transition (:issue:`9119`, :issue:`21459`)
- Bug in :meth:`DataFrame.expanding` in which the ``axis`` argument was not being respected during aggregations (:issue:`23372`)

Reshaping
^^^^^^^^^
Expand Down
25 changes: 19 additions & 6 deletions pandas/core/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -1866,12 +1866,25 @@ def _constructor(self):
return Expanding

def _get_window(self, other=None):
obj = self._selected_obj
if other is None:
return (max(len(obj), self.min_periods) if self.min_periods
else len(obj))
return (max((len(obj) + len(obj)), self.min_periods)
if self.min_periods else (len(obj) + len(obj)))
"""
Get the window length over which to perform some operation.
Parameters
----------
other : object, default None
The other object that is involved in the operation.
Such an object is involved for operations like covariance.
Returns
-------
window : int
The window length.
"""
axis = self.obj._get_axis(self.axis)
length = len(axis) + (other is not None) * len(axis)

other = self.min_periods or -1
return max(length, other)

_agg_doc = dedent("""
Examples
Expand Down
38 changes: 38 additions & 0 deletions pandas/tests/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,25 @@ def test_iter_raises(self, klass):
with pytest.raises(NotImplementedError):
iter(obj.rolling(2))

def test_rolling_axis(self, axis_frame):
# see gh-23372.
df = DataFrame(np.ones((10, 20)))
axis = df._get_axis_number(axis_frame)

if axis == 0:
expected = DataFrame({
i: [np.nan] * 2 + [3.0] * 8
for i in range(20)
})
else:
# axis == 1
expected = DataFrame([
[np.nan] * 2 + [3.0] * 18
] * 10)

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


class TestExpanding(Base):

Expand Down Expand Up @@ -714,6 +733,25 @@ def test_iter_raises(self, klass):
with pytest.raises(NotImplementedError):
iter(obj.expanding(2))

def test_expanding_axis(self, axis_frame):
# see gh-23372.
df = DataFrame(np.ones((10, 20)))
axis = df._get_axis_number(axis_frame)

if axis == 0:
expected = DataFrame({
i: [np.nan] * 2 + [float(j) for j in range(3, 11)]
for i in range(20)
})
else:
# axis == 1
expected = DataFrame([
[np.nan] * 2 + [float(i) for i in range(3, 21)]
] * 10)

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


class TestEWM(Base):

Expand Down

0 comments on commit 45dc501

Please sign in to comment.