Skip to content

ENH: Add numeric_only to resampler methods #46792

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 7 commits into from
Apr 23, 2022
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add numeric_only to resampler
  • Loading branch information
lorentzbao committed Apr 22, 2022
commit a8701f2669fda546132dbbac11a980003b69ecbc
36 changes: 18 additions & 18 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,6 @@

_shared_docs_kwargs: dict[str, str] = {}

_resample_agg_method_template = """
Compute {fname} of group values.

Parameters
----------
min_count : int, default {mc}
The required number of valid values to perform the operation. If fewer
than ``min_count`` non-NA values are present the result will be NA.

Returns
-------
Series or DataFrame
Computed {fname} of values within each group.
"""


class Resampler(BaseGroupBy, PandasObject):
"""
Expand Down Expand Up @@ -1042,11 +1027,26 @@ def quantile(self, q=0.5, **kwargs):
# downsample methods
for method in ["sum", "prod", "min", "max", "first", "last"]:

@doc(_resample_agg_method_template, fname=method, mc=0)
def f(self, _method=method, min_count=0, *args, **kwargs):
def f(
self,
_method: str = method,
numeric_only: bool | lib.NoDefault = lib.no_default,
min_count: int = 0,
*args,
**kwargs
):
if numeric_only is lib.no_default:
if self.obj.ndim == 1:
numeric_only = None
elif _method in ["first", "min"]:
numeric_only = False

nv.validate_resampler_func(_method, args, kwargs)
return self._downsample(_method, min_count=min_count)
return self._downsample(
_method, numeric_only=numeric_only, min_count=min_count
)

f.__doc__ = getattr(GroupBy, method).__doc__
setattr(Resampler, method, f)


Expand Down