Skip to content

BUG: Respect axis when doing DataFrame.expanding #23402

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 1 commit into from
Nov 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1238,6 +1238,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