Skip to content

CLN: Use kwargs instead of kwds in apply functions #39625

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

Merged
merged 1 commit into from
Feb 7, 2021
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
40 changes: 20 additions & 20 deletions pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def frame_apply(
raw: bool = False,
result_type: Optional[str] = None,
args=None,
kwds=None,
kwargs=None,
) -> FrameApply:
""" construct and return a row or column based frame apply object """
axis = obj._get_axis_number(axis)
Expand All @@ -79,7 +79,7 @@ def frame_apply(
raw=raw,
result_type=result_type,
args=args,
kwds=kwds,
kwargs=kwargs,
)


Expand All @@ -88,14 +88,14 @@ def series_apply(
func: AggFuncType,
convert_dtype: bool = True,
args=None,
kwds=None,
kwargs=None,
) -> SeriesApply:
return SeriesApply(
obj,
func,
convert_dtype,
args,
kwds,
kwargs,
)


Expand All @@ -109,12 +109,12 @@ def __init__(
raw: bool,
result_type: Optional[str],
args,
kwds,
kwargs,
):
self.obj = obj
self.raw = raw
self.args = args or ()
self.kwds = kwds or {}
self.kwargs = kwargs or {}

if result_type not in [None, "reduce", "broadcast", "expand"]:
raise ValueError(
Expand All @@ -126,13 +126,13 @@ def __init__(

# curry if needed
if (
(kwds or args)
(kwargs or args)
and not isinstance(func, (np.ufunc, str))
and not is_list_like(func)
):

def f(x):
return func(x, *args, **kwds)
return func(x, *args, **kwargs)

else:
f = func
Expand Down Expand Up @@ -163,7 +163,7 @@ def agg(self) -> Tuple[Optional[FrameOrSeriesUnion], Optional[bool]]:
obj = self.obj
arg = self.f
args = self.args
kwargs = self.kwds
kwargs = self.kwargs

_axis = kwargs.pop("_axis", None)
if _axis is None:
Expand Down Expand Up @@ -413,10 +413,10 @@ def maybe_apply_str(self) -> Optional[FrameOrSeriesUnion]:
if callable(func):
sig = inspect.getfullargspec(func)
if "axis" in sig.args:
self.kwds["axis"] = self.axis
self.kwargs["axis"] = self.axis
elif self.axis != 0:
raise ValueError(f"Operation {f} does not support axis=1")
return self.obj._try_aggregate_string_function(f, *self.args, **self.kwds)
return self.obj._try_aggregate_string_function(f, *self.args, **self.kwargs)

def maybe_apply_multiple(self) -> Optional[FrameOrSeriesUnion]:
"""
Expand All @@ -430,7 +430,7 @@ def maybe_apply_multiple(self) -> Optional[FrameOrSeriesUnion]:
# Note: dict-likes are list-like
if not is_list_like(self.f):
return None
return self.obj.aggregate(self.f, self.axis, *self.args, **self.kwds)
return self.obj.aggregate(self.f, self.axis, *self.args, **self.kwargs)


class FrameApply(Apply):
Expand Down Expand Up @@ -806,7 +806,7 @@ def __init__(
func: AggFuncType,
convert_dtype: bool,
args,
kwds,
kwargs,
):
self.convert_dtype = convert_dtype

Expand All @@ -816,7 +816,7 @@ def __init__(
raw=False,
result_type=None,
args=args,
kwds=kwds,
kwargs=kwargs,
)

def apply(self) -> FrameOrSeriesUnion:
Expand Down Expand Up @@ -877,17 +877,17 @@ def __init__(
obj: Union[SeriesGroupBy, DataFrameGroupBy],
func: AggFuncType,
args,
kwds,
kwargs,
):
kwds = kwds.copy()
self.axis = obj.obj._get_axis_number(kwds.get("axis", 0))
kwargs = kwargs.copy()
self.axis = obj.obj._get_axis_number(kwargs.get("axis", 0))
super().__init__(
obj,
func,
raw=False,
result_type=None,
args=args,
kwds=kwds,
kwargs=kwargs,
)

def apply(self):
Expand All @@ -903,15 +903,15 @@ def __init__(
obj: Union[Resampler, BaseWindow],
func: AggFuncType,
args,
kwds,
kwargs,
):
super().__init__(
obj,
func,
raw=False,
result_type=None,
args=args,
kwds=kwds,
kwargs=kwargs,
)

def apply(self):
Expand Down
8 changes: 4 additions & 4 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -7718,7 +7718,7 @@ def _aggregate(self, arg, axis: Axis = 0, *args, **kwargs):
func=arg,
axis=0,
args=args,
kwds=kwargs,
kwargs=kwargs,
)
result, how = op.agg()

Expand Down Expand Up @@ -7750,7 +7750,7 @@ def apply(
raw: bool = False,
result_type=None,
args=(),
**kwds,
**kwargs,
):
"""
Apply a function along an axis of the DataFrame.
Expand Down Expand Up @@ -7798,7 +7798,7 @@ def apply(
args : tuple
Positional arguments to pass to `func` in addition to the
array/series.
**kwds
**kwargs
Additional keyword arguments to pass as keywords arguments to
`func`.

Expand Down Expand Up @@ -7892,7 +7892,7 @@ def apply(
raw=raw,
result_type=result_type,
args=args,
kwds=kwds,
kwargs=kwargs,
)
return op.apply()

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,7 @@ def aggregate(self, func=None, *args, engine=None, engine_kwargs=None, **kwargs)
# try to treat as if we are passing a list
try:
result, _ = GroupByApply(
self, [func], args=(), kwds={"_axis": self.axis}
self, [func], args=(), kwargs={"_axis": self.axis}
).agg()

# select everything except for the last level, which is the one
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ def pipe(
def aggregate(self, func, *args, **kwargs):

self._set_binner()
result, how = ResamplerWindowApply(self, func, args=args, kwds=kwargs).agg()
result, how = ResamplerWindowApply(self, func, args=args, kwargs=kwargs).agg()
if result is None:
how = func
grouper = None
Expand Down
8 changes: 4 additions & 4 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3940,7 +3940,7 @@ def aggregate(self, func=None, axis=0, *args, **kwargs):
if func is None:
func = dict(kwargs.items())

op = series_apply(self, func, args=args, kwds=kwargs)
op = series_apply(self, func, args=args, kwargs=kwargs)
result, how = op.agg()
if result is None:

Expand Down Expand Up @@ -3981,7 +3981,7 @@ def apply(
func: AggFuncType,
convert_dtype: bool = True,
args: Tuple[Any, ...] = (),
**kwds,
**kwargs,
) -> FrameOrSeriesUnion:
"""
Invoke function on values of Series.
Expand All @@ -3998,7 +3998,7 @@ def apply(
False, leave as dtype=object.
args : tuple
Positional arguments passed to func after the series value.
**kwds
**kwargs
Additional keyword arguments passed to func.

Returns
Expand Down Expand Up @@ -4079,7 +4079,7 @@ def apply(
Helsinki 2.484907
dtype: float64
"""
op = series_apply(self, func, convert_dtype, args, kwds)
op = series_apply(self, func, convert_dtype, args, kwargs)
return op.apply()

def _reduce(
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ def calc(x):
return self._apply_tablewise(homogeneous_func, name)

def aggregate(self, func, *args, **kwargs):
result, how = ResamplerWindowApply(self, func, args=args, kwds=kwargs).agg()
result, how = ResamplerWindowApply(self, func, args=args, kwargs=kwargs).agg()
if result is None:
return self.apply(func, raw=False, args=args, kwargs=kwargs)
return result
Expand Down Expand Up @@ -994,7 +994,7 @@ def calc(x):
axis="",
)
def aggregate(self, func, *args, **kwargs):
result, how = ResamplerWindowApply(self, func, args=args, kwds=kwargs).agg()
result, how = ResamplerWindowApply(self, func, args=args, kwargs=kwargs).agg()
if result is None:

# these must apply directly
Expand Down