Skip to content

Commit d1485e7

Browse files
authored
CLN: Refactor groupby._make_wrapper (#48400)
* CLN: Refactor groupby._make_wrapper * Remove type: ignore * Revert behavior change for corrwith; fix skew and mad * Add docstring
1 parent 84fa883 commit d1485e7

File tree

8 files changed

+548
-255
lines changed

8 files changed

+548
-255
lines changed

pandas/core/apply.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -550,10 +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:
554-
self.kwargs["axis"] = self.axis
555-
elif self.axis != 0:
553+
if self.axis != 0 and (
554+
"axis" not in sig.args or f in ("corrwith", "mad", "skew")
555+
):
556556
raise ValueError(f"Operation {f} does not support axis=1")
557+
elif "axis" in sig.args:
558+
self.kwargs["axis"] = self.axis
557559
return self._try_aggregate_string_function(obj, f, *self.args, **self.kwargs)
558560

559561
def apply_multiple(self) -> DataFrame | Series:

pandas/core/generic.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -11660,10 +11660,8 @@ def all(self, axis=0, bool_only=None, skipna=True, level=None, **kwargs):
1166011660

1166111661
setattr(cls, "all", all)
1166211662

11663-
# error: Argument 1 to "doc" has incompatible type "Optional[str]"; expected
11664-
# "Union[str, Callable[..., Any]]"
1166511663
@doc(
11666-
NDFrame.mad.__doc__, # type: ignore[arg-type]
11664+
NDFrame.mad.__doc__,
1166711665
desc="Return the mean absolute deviation of the values "
1166811666
"over the requested axis.",
1166911667
name1=name1,

pandas/core/groupby/base.py

+1-33
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
"""
2-
Provide basic components for groupby. These definitions
3-
hold the allowlist of methods that are exposed on the
4-
SeriesGroupBy and the DataFrameGroupBy objects.
2+
Provide basic components for groupby.
53
"""
64
from __future__ import annotations
75

@@ -22,36 +20,6 @@ class OutputKey:
2220
# forwarding methods from NDFrames
2321
plotting_methods = frozenset(["plot", "hist"])
2422

25-
common_apply_allowlist = (
26-
frozenset(
27-
[
28-
"quantile",
29-
"fillna",
30-
"mad",
31-
"take",
32-
"idxmax",
33-
"idxmin",
34-
"tshift",
35-
"skew",
36-
"corr",
37-
"cov",
38-
"diff",
39-
]
40-
)
41-
| plotting_methods
42-
)
43-
44-
series_apply_allowlist: frozenset[str] = (
45-
common_apply_allowlist
46-
| frozenset(
47-
{"nlargest", "nsmallest", "is_monotonic_increasing", "is_monotonic_decreasing"}
48-
)
49-
) | frozenset(["dtype", "unique"])
50-
51-
dataframe_apply_allowlist: frozenset[str] = common_apply_allowlist | frozenset(
52-
["dtypes", "corrwith"]
53-
)
54-
5523
# cythonized transformations or canned "agg+broadcast", which do not
5624
# require postprocessing of the result by transform.
5725
cythonized_kernels = frozenset(["cumprod", "cumsum", "shift", "cummin", "cummax"])

0 commit comments

Comments
 (0)