Skip to content

Typ numerical ops #44607

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
Nov 28, 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
195 changes: 148 additions & 47 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10298,7 +10298,14 @@ def pct_change(
return rs

@final
def _agg_by_level(self, name, axis=0, level=0, skipna=True, **kwargs):
def _agg_by_level(
self,
name: str,
axis: Axis = 0,
level: Level = 0,
skipna: bool_t = True,
**kwargs,
):
if axis is None:
raise ValueError("Must specify 'axis' when aggregating by level.")
grouped = self.groupby(level=level, axis=axis, sort=False)
Expand All @@ -10311,8 +10318,15 @@ def _agg_by_level(self, name, axis=0, level=0, skipna=True, **kwargs):

@final
def _logical_func(
self, name: str, func, axis=0, bool_only=None, skipna=True, level=None, **kwargs
):
self,
name: str,
func,
axis: Axis = 0,
bool_only: bool_t | None = None,
skipna: bool_t = True,
level: Level | None = None,
**kwargs,
) -> Series | bool_t:
nv.validate_logical_func((), kwargs, fname=name)
validate_bool_kwarg(skipna, "skipna", none_allowed=False)
if level is not None:
Expand Down Expand Up @@ -10345,18 +10359,40 @@ def _logical_func(
filter_type="bool",
)

def any(self, axis=0, bool_only=None, skipna=True, level=None, **kwargs):
def any(
self,
axis: Axis = 0,
bool_only: bool_t | None = None,
skipna: bool_t = True,
level: Level | None = None,
**kwargs,
) -> Series | bool_t:
return self._logical_func(
"any", nanops.nanany, axis, bool_only, skipna, level, **kwargs
)

def all(self, axis=0, bool_only=None, skipna=True, level=None, **kwargs):
def all(
self,
axis: Axis = 0,
bool_only: bool_t | None = None,
skipna: bool_t = True,
level: Level | None = None,
**kwargs,
) -> Series | bool_t:
return self._logical_func(
"all", nanops.nanall, axis, bool_only, skipna, level, **kwargs
)

@final
def _accum_func(self, name: str, func, axis=None, skipna=True, *args, **kwargs):
def _accum_func(
self,
name: str,
func,
axis: Axis | None = None,
skipna: bool_t = True,
*args,
**kwargs,
):
skipna = nv.validate_cum_func_with_skipna(skipna, args, kwargs, name)
if axis is None:
axis = self._stat_axis_number
Expand All @@ -10380,34 +10416,34 @@ def block_accum_func(blk_values):

return self._constructor(result).__finalize__(self, method=name)

def cummax(self, axis=None, skipna=True, *args, **kwargs):
def cummax(self, axis: Axis | None = None, skipna: bool_t = True, *args, **kwargs):
return self._accum_func(
"cummax", np.maximum.accumulate, axis, skipna, *args, **kwargs
)

def cummin(self, axis=None, skipna=True, *args, **kwargs):
def cummin(self, axis: Axis | None = None, skipna: bool_t = True, *args, **kwargs):
return self._accum_func(
"cummin", np.minimum.accumulate, axis, skipna, *args, **kwargs
)

def cumsum(self, axis=None, skipna=True, *args, **kwargs):
def cumsum(self, axis: Axis | None = None, skipna: bool_t = True, *args, **kwargs):
return self._accum_func("cumsum", np.cumsum, axis, skipna, *args, **kwargs)

def cumprod(self, axis=None, skipna=True, *args, **kwargs):
def cumprod(self, axis: Axis | None = None, skipna: bool_t = True, *args, **kwargs):
return self._accum_func("cumprod", np.cumprod, axis, skipna, *args, **kwargs)

@final
def _stat_function_ddof(
self,
name: str,
func,
axis=None,
skipna=True,
level=None,
ddof=1,
numeric_only=None,
axis: Axis | None = None,
skipna: bool_t = True,
level: Level | None = None,
ddof: int = 1,
numeric_only: bool_t | None = None,
**kwargs,
):
) -> Series | float:
nv.validate_stat_ddof_func((), kwargs, fname=name)
validate_bool_kwarg(skipna, "skipna", none_allowed=False)
if axis is None:
Expand All @@ -10428,22 +10464,40 @@ def _stat_function_ddof(
)

def sem(
self, axis=None, skipna=True, level=None, ddof=1, numeric_only=None, **kwargs
):
self,
axis: Axis | None = None,
skipna: bool_t = True,
level: Level | None = None,
ddof: int = 1,
numeric_only: bool_t | None = None,
**kwargs,
) -> Series | float:
return self._stat_function_ddof(
"sem", nanops.nansem, axis, skipna, level, ddof, numeric_only, **kwargs
)

def var(
self, axis=None, skipna=True, level=None, ddof=1, numeric_only=None, **kwargs
):
self,
axis: Axis | None = None,
skipna: bool_t = True,
level: Level | None = None,
ddof: int = 1,
numeric_only: bool_t | None = None,
**kwargs,
) -> Series | float:
return self._stat_function_ddof(
"var", nanops.nanvar, axis, skipna, level, ddof, numeric_only, **kwargs
)

def std(
self, axis=None, skipna=True, level=None, ddof=1, numeric_only=None, **kwargs
):
self,
axis: Axis | None = None,
skipna: bool_t = True,
level: Level | None = None,
ddof: int = 1,
numeric_only: bool_t | None = None,
**kwargs,
) -> Series | float:
return self._stat_function_ddof(
"std", nanops.nanstd, axis, skipna, level, ddof, numeric_only, **kwargs
)
Expand All @@ -10453,10 +10507,10 @@ def _stat_function(
self,
name: str,
func,
axis=None,
skipna=True,
level=None,
numeric_only=None,
axis: Axis | None = None,
skipna: bool_t = True,
level: Level | None = None,
numeric_only: bool_t | None = None,
**kwargs,
):
if name == "median":
Expand All @@ -10483,32 +10537,74 @@ def _stat_function(
func, name=name, axis=axis, skipna=skipna, numeric_only=numeric_only
)

def min(self, axis=None, skipna=True, level=None, numeric_only=None, **kwargs):
def min(
self,
axis: Axis | None = None,
skipna: bool_t = True,
level: Level | None = None,
numeric_only: bool_t | None = None,
**kwargs,
):
return self._stat_function(
"min", nanops.nanmin, axis, skipna, level, numeric_only, **kwargs
)

def max(self, axis=None, skipna=True, level=None, numeric_only=None, **kwargs):
def max(
self,
axis: Axis | None = None,
skipna: bool_t = True,
level: Level | None = None,
numeric_only: bool_t | None = None,
**kwargs,
):
return self._stat_function(
"max", nanops.nanmax, axis, skipna, level, numeric_only, **kwargs
)

def mean(self, axis=None, skipna=True, level=None, numeric_only=None, **kwargs):
def mean(
self,
axis: Axis | None = None,
skipna: bool_t = True,
level: Level | None = None,
numeric_only: bool_t | None = None,
**kwargs,
) -> Series | float:
return self._stat_function(
"mean", nanops.nanmean, axis, skipna, level, numeric_only, **kwargs
)

def median(self, axis=None, skipna=True, level=None, numeric_only=None, **kwargs):
def median(
self,
axis: Axis | None = None,
skipna: bool_t = True,
level: Level | None = None,
numeric_only: bool_t | None = None,
**kwargs,
) -> Series | float:
return self._stat_function(
"median", nanops.nanmedian, axis, skipna, level, numeric_only, **kwargs
)

def skew(self, axis=None, skipna=True, level=None, numeric_only=None, **kwargs):
def skew(
self,
axis: Axis | None = None,
skipna: bool_t = True,
level: Level | None = None,
numeric_only: bool_t | None = None,
**kwargs,
) -> Series | float:
return self._stat_function(
"skew", nanops.nanskew, axis, skipna, level, numeric_only, **kwargs
)

def kurt(self, axis=None, skipna=True, level=None, numeric_only=None, **kwargs):
def kurt(
self,
axis: Axis | None = None,
skipna: bool_t = True,
level: Level | None = None,
numeric_only: bool_t | None = None,
**kwargs,
) -> Series | float:
return self._stat_function(
"kurt", nanops.nankurt, axis, skipna, level, numeric_only, **kwargs
)
Expand All @@ -10520,11 +10616,11 @@ def _min_count_stat_function(
self,
name: str,
func,
axis=None,
skipna=True,
level=None,
numeric_only=None,
min_count=0,
axis: Axis | None = None,
skipna: bool_t = True,
level: Level | None = None,
numeric_only: bool_t | None = None,
min_count: int = 0,
**kwargs,
):
if name == "sum":
Expand Down Expand Up @@ -10565,10 +10661,10 @@ def _min_count_stat_function(

def sum(
self,
axis=None,
skipna=True,
level=None,
numeric_only=None,
axis: Axis | None = None,
skipna: bool_t = True,
level: Level | None = None,
numeric_only: bool_t | None = None,
min_count=0,
**kwargs,
):
Expand All @@ -10578,11 +10674,11 @@ def sum(

def prod(
self,
axis=None,
skipna=True,
level=None,
numeric_only=None,
min_count=0,
axis: Axis | None = None,
skipna: bool_t = True,
level: Level | None = None,
numeric_only: bool_t | None = None,
min_count: int = 0,
**kwargs,
):
return self._min_count_stat_function(
Expand All @@ -10598,7 +10694,12 @@ def prod(

product = prod

def mad(self, axis=None, skipna=True, level=None):
def mad(
self,
axis: Axis | None = None,
skipna: bool_t = True,
level: Level | None = None,
) -> Series | float:
"""
{desc}

Expand Down
12 changes: 6 additions & 6 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1931,7 +1931,7 @@ def mean(
2 4.0
Name: B, dtype: float64
"""
numeric_only = self._resolve_numeric_only(numeric_only)
numeric_only_bool = self._resolve_numeric_only(numeric_only)

if maybe_use_numba(engine):
from pandas.core._numba.kernels import sliding_mean
Expand All @@ -1940,8 +1940,8 @@ def mean(
else:
result = self._cython_agg_general(
"mean",
alt=lambda x: Series(x).mean(numeric_only=numeric_only),
numeric_only=numeric_only,
alt=lambda x: Series(x).mean(numeric_only=numeric_only_bool),
numeric_only=numeric_only_bool,
)
return result.__finalize__(self.obj, method="groupby")

Expand All @@ -1965,12 +1965,12 @@ def median(self, numeric_only: bool | lib.NoDefault = lib.no_default):
Series or DataFrame
Median of values within each group.
"""
numeric_only = self._resolve_numeric_only(numeric_only)
numeric_only_bool = self._resolve_numeric_only(numeric_only)

result = self._cython_agg_general(
"median",
alt=lambda x: Series(x).median(numeric_only=numeric_only),
numeric_only=numeric_only,
alt=lambda x: Series(x).median(numeric_only=numeric_only_bool),
numeric_only=numeric_only_bool,
)
return result.__finalize__(self.obj, method="groupby")

Expand Down