Skip to content

Commit eba9499

Browse files
committed
REGR: fix df.apply with keyword non-zero axis
Fixes pandas-dev#48656. Builds on top of pandas-dev#48693. An alternative fix could be to use `inspect.signature`, since `inspect.signature` defaults to following wrapped functions. But this seems like a more minimal change.
1 parent 8e34794 commit eba9499

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

pandas/core/apply.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,10 +552,11 @@ def apply_str(self) -> DataFrame | Series:
552552
if callable(func):
553553
sig = inspect.getfullargspec(func)
554554
if self.axis != 0 and (
555-
"axis" not in sig.args or f in ("corrwith", "mad", "skew")
555+
("axis" not in sig.args and "axis" not in sig.kwonlyargs)
556+
or f in ("corrwith", "mad", "skew")
556557
):
557558
raise ValueError(f"Operation {f} does not support axis=1")
558-
elif "axis" in sig.args:
559+
elif "axis" in sig.args or "axis" in sig.kwonlyargs:
559560
self.kwargs["axis"] = self.axis
560561
return self._try_aggregate_string_function(obj, f, *self.args, **self.kwargs)
561562

pandas/tests/groupby/test_any_all.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,23 @@ def test_any_non_keyword_deprecation():
8383
tm.assert_equal(result, expected)
8484

8585

86+
def test_any_apply_keyword_non_zero_axis_regression():
87+
# https://github.com/pandas-dev/pandas/issues/48656
88+
df = DataFrame({"A": [1, 2], "B": [0, 2], "C": [0, 0]})
89+
expected = df.any(axis=1)
90+
result = df.apply('any', axis=1)
91+
tm.assert_series_equal(result, expected)
92+
93+
msg = (
94+
"In a future version of pandas all arguments of "
95+
"DataFrame.any and Series.any will be keyword-only."
96+
)
97+
with tm.assert_produces_warning(FutureWarning, match=msg):
98+
expected = df.any(1)
99+
result = df.apply('any', 1)
100+
tm.assert_series_equal(result, expected)
101+
102+
86103
@pytest.mark.parametrize("bool_agg_func", ["any", "all"])
87104
def test_bool_aggs_dup_column_labels(bool_agg_func):
88105
# 21668

0 commit comments

Comments
 (0)