Skip to content
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

[ArrayManager] Fix window operations with axis=1 #40251

Merged
merged 1 commit into from
Mar 5, 2021
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 .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,4 @@ jobs:
pytest pandas/tests/tseries/
pytest pandas/tests/tslibs/
pytest pandas/tests/util/
pytest pandas/tests/window/
2 changes: 1 addition & 1 deletion pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def add_imports(doctest_namespace):
# ----------------------------------------------------------------
# Common arguments
# ----------------------------------------------------------------
@pytest.fixture(params=[0, 1, "index", "columns"], ids=lambda x: f"axis {repr(x)}")
@pytest.fixture(params=[0, 1, "index", "columns"], ids=lambda x: f"axis={repr(x)}")
def axis(request):
"""
Fixture for returning the axis numbers of a DataFrame.
Expand Down
24 changes: 24 additions & 0 deletions pandas/core/internals/array_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,30 @@ def apply(

return type(self)(result_arrays, new_axes)

def apply_2d(
self: T,
f,
ignore_failures: bool = False,
**kwargs,
) -> T:
"""
Variant of `apply`, but where the function should not be applied to
each column independently, but to the full data as a 2D array.
"""
values = self.as_array()
try:
result = f(values, **kwargs)
except (TypeError, NotImplementedError):
if not ignore_failures:
raise
result_arrays = []
new_axes = [self._axes[0], self.axes[1].take([])]
else:
result_arrays = [result[:, i] for i in range(len(self._axes[1]))]
new_axes = self._axes

return type(self)(result_arrays, new_axes)

def apply_with_block(self: T, f, align_keys=None, **kwargs) -> T:

align_keys = align_keys or []
Expand Down
10 changes: 9 additions & 1 deletion pandas/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
Index,
MultiIndex,
)
from pandas.core.internals import ArrayManager
from pandas.core.reshape.concat import concat
from pandas.core.util.numba_ import (
NUMBA_FUNC_CACHE,
Expand Down Expand Up @@ -410,7 +411,14 @@ def hfunc(bvalues: ArrayLike) -> ArrayLike:
res_values = homogeneous_func(values)
return getattr(res_values, "T", res_values)

new_mgr = mgr.apply(hfunc, ignore_failures=True)
def hfunc2d(values: ArrayLike) -> ArrayLike:
values = self._prep_values(values)
return homogeneous_func(values)

if isinstance(mgr, ArrayManager) and self.axis == 1:
new_mgr = mgr.apply_2d(hfunc2d, ignore_failures=True)
else:
new_mgr = mgr.apply(hfunc, ignore_failures=True)
out = obj._constructor(new_mgr)

if out.shape[1] == 0 and obj.shape[1] > 0:
Expand Down
10 changes: 6 additions & 4 deletions pandas/tests/window/test_rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ def test_rolling_datetime(axis_frame, tz_naive_fixture):
tm.assert_frame_equal(result, expected)


def test_rolling_window_as_string():
def test_rolling_window_as_string(using_array_manager):
# see gh-22590
date_today = datetime.now()
days = date_range(date_today, date_today + timedelta(365), freq="D")
Expand Down Expand Up @@ -450,9 +450,11 @@ def test_rolling_window_as_string():
+ [95.0] * 20
)

expected = Series(
expData, index=days.rename("DateCol")._with_freq(None), name="metric"
)
index = days.rename("DateCol")
if not using_array_manager:
# INFO(ArrayManager) preserves the frequence of the index
index = index._with_freq(None)
expected = Series(expData, index=index, name="metric")
tm.assert_series_equal(result, expected)


Expand Down