Skip to content

Commit 278c182

Browse files
phoflhauntsaninja
andauthored
Backport PR #48797 on branch 1.5.x (REGR: fix df.apply with keyword non-zero axis) (#48886)
REGR: fix df.apply with keyword non-zero axis (#48797) Co-authored-by: Shantanu <[email protected]>
1 parent a1b3880 commit 278c182

File tree

4 files changed

+42
-23
lines changed

4 files changed

+42
-23
lines changed

doc/source/whatsnew/v1.5.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ Fixed regressions
8282
- Fixed regression in :meth:`DataFrame.fillna` replacing wrong values for ``datetime64[ns]`` dtype and ``inplace=True`` (:issue:`48863`)
8383
- Fixed :meth:`.DataFrameGroupBy.size` not returning a Series when ``axis=1`` (:issue:`48738`)
8484
- Fixed Regression in :meth:`DataFrameGroupBy.apply` when user defined function is called on an empty dataframe (:issue:`47985`)
85+
- Fixed regression in :meth:`DataFrame.apply` when passing non-zero ``axis`` via keyword argument (:issue:`48656`)
8586
-
8687

8788
.. ---------------------------------------------------------------------------

pandas/core/apply.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,12 @@ def apply_str(self) -> DataFrame | Series:
550550
func = getattr(obj, f, None)
551551
if callable(func):
552552
sig = inspect.getfullargspec(func)
553-
if "axis" in sig.args:
553+
arg_names = (*sig.args, *sig.kwonlyargs)
554+
if self.axis != 0 and (
555+
"axis" not in arg_names or f in ("corrwith", "mad", "skew")
556+
):
557+
raise ValueError(f"Operation {f} does not support axis=1")
558+
elif "axis" in arg_names:
554559
self.kwargs["axis"] = self.axis
555560
elif self.axis != 0:
556561
raise ValueError(f"Operation {f} does not support axis=1")

pandas/tests/apply/test_frame_apply.py

+35
Original file line numberDiff line numberDiff line change
@@ -1604,3 +1604,38 @@ def test_unique_agg_type_is_series(test, constant):
16041604
result = df1.agg(aggregation)
16051605

16061606
tm.assert_series_equal(result, expected)
1607+
1608+
1609+
def test_any_non_keyword_deprecation():
1610+
df = DataFrame({"A": [1, 2], "B": [0, 2], "C": [0, 0]})
1611+
msg = (
1612+
"In a future version of pandas all arguments of "
1613+
"DataFrame.any and Series.any will be keyword-only."
1614+
)
1615+
with tm.assert_produces_warning(FutureWarning, match=msg):
1616+
result = df.any("index", None)
1617+
expected = Series({"A": True, "B": True, "C": False})
1618+
tm.assert_series_equal(result, expected)
1619+
1620+
s = Series([False, False, False])
1621+
msg = (
1622+
"In a future version of pandas all arguments of "
1623+
"DataFrame.any and Series.any will be keyword-only."
1624+
)
1625+
with tm.assert_produces_warning(FutureWarning, match=msg):
1626+
result = s.any("index")
1627+
expected = False
1628+
tm.assert_equal(result, expected)
1629+
1630+
1631+
def test_any_apply_keyword_non_zero_axis_regression():
1632+
# https://github.com/pandas-dev/pandas/issues/48656
1633+
df = DataFrame({"A": [1, 2, 0], "B": [0, 2, 0], "C": [0, 0, 0]})
1634+
expected = Series([True, True, False])
1635+
tm.assert_series_equal(df.any(axis=1), expected)
1636+
1637+
result = df.apply("any", axis=1)
1638+
tm.assert_series_equal(result, expected)
1639+
1640+
result = df.apply("any", 1)
1641+
tm.assert_series_equal(result, expected)

pandas/tests/groupby/test_any_all.py

-22
Original file line numberDiff line numberDiff line change
@@ -61,28 +61,6 @@ def test_any():
6161
tm.assert_frame_equal(result, expected)
6262

6363

64-
def test_any_non_keyword_deprecation():
65-
df = DataFrame({"A": [1, 2], "B": [0, 2], "C": [0, 0]})
66-
msg = (
67-
"In a future version of pandas all arguments of "
68-
"DataFrame.any and Series.any will be keyword-only."
69-
)
70-
with tm.assert_produces_warning(FutureWarning, match=msg):
71-
result = df.any("index", None)
72-
expected = Series({"A": True, "B": True, "C": False})
73-
tm.assert_series_equal(result, expected)
74-
75-
s = Series([False, False, False])
76-
msg = (
77-
"In a future version of pandas all arguments of "
78-
"DataFrame.any and Series.any will be keyword-only."
79-
)
80-
with tm.assert_produces_warning(FutureWarning, match=msg):
81-
result = s.any("index")
82-
expected = False
83-
tm.assert_equal(result, expected)
84-
85-
8664
@pytest.mark.parametrize("bool_agg_func", ["any", "all"])
8765
def test_bool_aggs_dup_column_labels(bool_agg_func):
8866
# 21668

0 commit comments

Comments
 (0)