Skip to content

Commit

Permalink
REGR: fix df.apply with keyword non-zero axis (pandas-dev#48797)
Browse files Browse the repository at this point in the history
  • Loading branch information
hauntsaninja authored and noatamir committed Nov 9, 2022
1 parent dee90b7 commit 9762f10
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 24 deletions.
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v1.5.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ 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
5 changes: 3 additions & 2 deletions pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,11 +555,12 @@ def apply_str(self) -> DataFrame | Series:
func = getattr(obj, f, None)
if callable(func):
sig = inspect.getfullargspec(func)
arg_names = (*sig.args, *sig.kwonlyargs)
if self.axis != 0 and (
"axis" not in sig.args or f in ("corrwith", "mad", "skew")
"axis" not in arg_names or f in ("corrwith", "mad", "skew")
):
raise ValueError(f"Operation {f} does not support axis=1")
elif "axis" in sig.args:
elif "axis" in arg_names:
self.kwargs["axis"] = self.axis
return self._try_aggregate_string_function(obj, f, *self.args, **self.kwargs)

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

0 comments on commit 9762f10

Please sign in to comment.