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

REGR: fix df.apply with keyword non-zero axis #48797

Merged
merged 8 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 3 additions & 2 deletions pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,10 +552,11 @@ def apply_str(self) -> DataFrame | Series:
if callable(func):
sig = inspect.getfullargspec(func)
if self.axis != 0 and (
"axis" not in sig.args or f in ("corrwith", "mad", "skew")
("axis" not in sig.args and "axis" not in sig.kwonlyargs)
MarcoGorelli marked this conversation as resolved.
Show resolved Hide resolved
or f in ("corrwith", "mad", "skew")
):
raise ValueError(f"Operation {f} does not support axis=1")
elif "axis" in sig.args:
elif "axis" in sig.args or "axis" in sig.kwonlyargs:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similar to above

self.kwargs["axis"] = self.axis
return self._try_aggregate_string_function(obj, f, *self.args, **self.kwargs)

Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/groupby/test_any_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,23 @@ def test_any_non_keyword_deprecation():
tm.assert_equal(result, expected)


def test_any_apply_keyword_non_zero_axis_regression():
# https://github.com/pandas-dev/pandas/issues/48656
hauntsaninja marked this conversation as resolved.
Show resolved Hide resolved
df = DataFrame({"A": [1, 2], "B": [0, 2], "C": [0, 0]})
expected = df.any(axis=1)
MarcoGorelli marked this conversation as resolved.
Show resolved Hide resolved
result = df.apply("any", axis=1)
tm.assert_series_equal(result, expected)

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):
expected = df.any(1)
hauntsaninja marked this conversation as resolved.
Show resolved Hide resolved
result = df.apply("any", 1)
tm.assert_series_equal(result, expected)


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