Skip to content

BUG: rolling(axis=1).apply() raising ValueError #45925

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 3 commits into from
Feb 11, 2022
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/v1.4.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Bug fixes
- Stopped emitting unnecessary ``FutureWarning`` in :meth:`DataFrame.sort_values` with sparse columns (:issue:`45618`)
- Fixed window aggregations in :meth:`DataFrame.rolling` and :meth:`Series.rolling` to skip over unused elements (:issue:`45647`)
- Fixed builtin highlighters in :class:`.Styler` to be responsive to ``NA`` with nullable dtypes (:issue:`45804`)
- Bug in :meth:`~Rolling.apply` with ``axis=1`` raising an erroneous ``ValueError`` (:issue:`45912`)

.. ---------------------------------------------------------------------------

Expand Down
3 changes: 2 additions & 1 deletion pandas/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -1343,7 +1343,8 @@ def _generate_cython_apply_func(

def apply_func(values, begin, end, min_periods, raw=raw):
if not raw:
values = Series(values, index=self.obj.index)
# GH 45912
values = Series(values, index=self._on)
return window_func(values, begin, end, min_periods)

return apply_func
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/window/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,11 @@ def test_center_reindex_frame(raw, frame):
)
frame_rs = frame.rolling(window=25, min_periods=minp, center=True).apply(f, raw=raw)
tm.assert_frame_equal(frame_xp, frame_rs)


def test_axis1(raw):
# GH 45912
df = DataFrame([1, 2])
result = df.rolling(window=1, axis=1).apply(np.sum, raw=raw)
expected = DataFrame([1.0, 2.0])
tm.assert_frame_equal(result, expected)