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

Backport PR #48797 on branch 1.5.x (REGR: fix df.apply with keyword non-zero axis) #48886

Merged
merged 1 commit into from
Sep 30, 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.5.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ Fixed regressions
- Fixed regression in :meth:`DataFrame.fillna` replacing wrong values for ``datetime64[ns]`` dtype and ``inplace=True`` (:issue:`48863`)
- Fixed :meth:`.DataFrameGroupBy.size` not returning a Series when ``axis=1`` (:issue:`48738`)
- Fixed Regression in :meth:`DataFrameGroupBy.apply` when user defined function is called on an empty dataframe (:issue:`47985`)
- Fixed regression in :meth:`DataFrame.apply` when passing non-zero ``axis`` via keyword argument (:issue:`48656`)
-

.. ---------------------------------------------------------------------------
Expand Down
7 changes: 6 additions & 1 deletion pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,12 @@ def apply_str(self) -> DataFrame | Series:
func = getattr(obj, f, None)
if callable(func):
sig = inspect.getfullargspec(func)
if "axis" in sig.args:
arg_names = (*sig.args, *sig.kwonlyargs)
if self.axis != 0 and (
"axis" not in arg_names or f in ("corrwith", "mad", "skew")
):
raise ValueError(f"Operation {f} does not support axis=1")
elif "axis" in arg_names:
self.kwargs["axis"] = self.axis
elif self.axis != 0:
raise ValueError(f"Operation {f} does not support axis=1")
Expand Down
35 changes: 35 additions & 0 deletions pandas/tests/apply/test_frame_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -1604,3 +1604,38 @@ def test_unique_agg_type_is_series(test, constant):
result = df1.agg(aggregation)

tm.assert_series_equal(result, expected)


def test_any_non_keyword_deprecation():
df = DataFrame({"A": [1, 2], "B": [0, 2], "C": [0, 0]})
msg = (
"In a future version of pandas all arguments of "
"DataFrame.any and Series.any will be keyword-only."
)
with tm.assert_produces_warning(FutureWarning, match=msg):
result = df.any("index", None)
expected = Series({"A": True, "B": True, "C": False})
tm.assert_series_equal(result, expected)

s = Series([False, False, False])
msg = (
"In a future version of pandas all arguments of "
"DataFrame.any and Series.any will be keyword-only."
)
with tm.assert_produces_warning(FutureWarning, match=msg):
result = s.any("index")
expected = False
tm.assert_equal(result, expected)


def test_any_apply_keyword_non_zero_axis_regression():
# https://github.com/pandas-dev/pandas/issues/48656
df = DataFrame({"A": [1, 2, 0], "B": [0, 2, 0], "C": [0, 0, 0]})
expected = Series([True, True, False])
tm.assert_series_equal(df.any(axis=1), expected)

result = df.apply("any", axis=1)
tm.assert_series_equal(result, expected)

result = df.apply("any", 1)
tm.assert_series_equal(result, expected)
22 changes: 0 additions & 22 deletions pandas/tests/groupby/test_any_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,6 @@ def test_any():
tm.assert_frame_equal(result, expected)


def test_any_non_keyword_deprecation():
df = DataFrame({"A": [1, 2], "B": [0, 2], "C": [0, 0]})
msg = (
"In a future version of pandas all arguments of "
"DataFrame.any and Series.any will be keyword-only."
)
with tm.assert_produces_warning(FutureWarning, match=msg):
result = df.any("index", None)
expected = Series({"A": True, "B": True, "C": False})
tm.assert_series_equal(result, expected)

s = Series([False, False, False])
msg = (
"In a future version of pandas all arguments of "
"DataFrame.any and Series.any will be keyword-only."
)
with tm.assert_produces_warning(FutureWarning, match=msg):
result = s.any("index")
expected = False
tm.assert_equal(result, expected)


@pytest.mark.parametrize("bool_agg_func", ["any", "all"])
def test_bool_aggs_dup_column_labels(bool_agg_func):
# 21668
Expand Down