Skip to content

Commit 6b53d07

Browse files
authored
Typ numerical ops (#44607)
1 parent d827c83 commit 6b53d07

File tree

2 files changed

+154
-53
lines changed

2 files changed

+154
-53
lines changed

pandas/core/generic.py

+148-47
Original file line numberDiff line numberDiff line change
@@ -10298,7 +10298,14 @@ def pct_change(
1029810298
return rs
1029910299

1030010300
@final
10301-
def _agg_by_level(self, name, axis=0, level=0, skipna=True, **kwargs):
10301+
def _agg_by_level(
10302+
self,
10303+
name: str,
10304+
axis: Axis = 0,
10305+
level: Level = 0,
10306+
skipna: bool_t = True,
10307+
**kwargs,
10308+
):
1030210309
if axis is None:
1030310310
raise ValueError("Must specify 'axis' when aggregating by level.")
1030410311
grouped = self.groupby(level=level, axis=axis, sort=False)
@@ -10311,8 +10318,15 @@ def _agg_by_level(self, name, axis=0, level=0, skipna=True, **kwargs):
1031110318

1031210319
@final
1031310320
def _logical_func(
10314-
self, name: str, func, axis=0, bool_only=None, skipna=True, level=None, **kwargs
10315-
):
10321+
self,
10322+
name: str,
10323+
func,
10324+
axis: Axis = 0,
10325+
bool_only: bool_t | None = None,
10326+
skipna: bool_t = True,
10327+
level: Level | None = None,
10328+
**kwargs,
10329+
) -> Series | bool_t:
1031610330
nv.validate_logical_func((), kwargs, fname=name)
1031710331
validate_bool_kwarg(skipna, "skipna", none_allowed=False)
1031810332
if level is not None:
@@ -10345,18 +10359,40 @@ def _logical_func(
1034510359
filter_type="bool",
1034610360
)
1034710361

10348-
def any(self, axis=0, bool_only=None, skipna=True, level=None, **kwargs):
10362+
def any(
10363+
self,
10364+
axis: Axis = 0,
10365+
bool_only: bool_t | None = None,
10366+
skipna: bool_t = True,
10367+
level: Level | None = None,
10368+
**kwargs,
10369+
) -> Series | bool_t:
1034910370
return self._logical_func(
1035010371
"any", nanops.nanany, axis, bool_only, skipna, level, **kwargs
1035110372
)
1035210373

10353-
def all(self, axis=0, bool_only=None, skipna=True, level=None, **kwargs):
10374+
def all(
10375+
self,
10376+
axis: Axis = 0,
10377+
bool_only: bool_t | None = None,
10378+
skipna: bool_t = True,
10379+
level: Level | None = None,
10380+
**kwargs,
10381+
) -> Series | bool_t:
1035410382
return self._logical_func(
1035510383
"all", nanops.nanall, axis, bool_only, skipna, level, **kwargs
1035610384
)
1035710385

1035810386
@final
10359-
def _accum_func(self, name: str, func, axis=None, skipna=True, *args, **kwargs):
10387+
def _accum_func(
10388+
self,
10389+
name: str,
10390+
func,
10391+
axis: Axis | None = None,
10392+
skipna: bool_t = True,
10393+
*args,
10394+
**kwargs,
10395+
):
1036010396
skipna = nv.validate_cum_func_with_skipna(skipna, args, kwargs, name)
1036110397
if axis is None:
1036210398
axis = self._stat_axis_number
@@ -10380,34 +10416,34 @@ def block_accum_func(blk_values):
1038010416

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

10383-
def cummax(self, axis=None, skipna=True, *args, **kwargs):
10419+
def cummax(self, axis: Axis | None = None, skipna: bool_t = True, *args, **kwargs):
1038410420
return self._accum_func(
1038510421
"cummax", np.maximum.accumulate, axis, skipna, *args, **kwargs
1038610422
)
1038710423

10388-
def cummin(self, axis=None, skipna=True, *args, **kwargs):
10424+
def cummin(self, axis: Axis | None = None, skipna: bool_t = True, *args, **kwargs):
1038910425
return self._accum_func(
1039010426
"cummin", np.minimum.accumulate, axis, skipna, *args, **kwargs
1039110427
)
1039210428

10393-
def cumsum(self, axis=None, skipna=True, *args, **kwargs):
10429+
def cumsum(self, axis: Axis | None = None, skipna: bool_t = True, *args, **kwargs):
1039410430
return self._accum_func("cumsum", np.cumsum, axis, skipna, *args, **kwargs)
1039510431

10396-
def cumprod(self, axis=None, skipna=True, *args, **kwargs):
10432+
def cumprod(self, axis: Axis | None = None, skipna: bool_t = True, *args, **kwargs):
1039710433
return self._accum_func("cumprod", np.cumprod, axis, skipna, *args, **kwargs)
1039810434

1039910435
@final
1040010436
def _stat_function_ddof(
1040110437
self,
1040210438
name: str,
1040310439
func,
10404-
axis=None,
10405-
skipna=True,
10406-
level=None,
10407-
ddof=1,
10408-
numeric_only=None,
10440+
axis: Axis | None = None,
10441+
skipna: bool_t = True,
10442+
level: Level | None = None,
10443+
ddof: int = 1,
10444+
numeric_only: bool_t | None = None,
1040910445
**kwargs,
10410-
):
10446+
) -> Series | float:
1041110447
nv.validate_stat_ddof_func((), kwargs, fname=name)
1041210448
validate_bool_kwarg(skipna, "skipna", none_allowed=False)
1041310449
if axis is None:
@@ -10428,22 +10464,40 @@ def _stat_function_ddof(
1042810464
)
1042910465

1043010466
def sem(
10431-
self, axis=None, skipna=True, level=None, ddof=1, numeric_only=None, **kwargs
10432-
):
10467+
self,
10468+
axis: Axis | None = None,
10469+
skipna: bool_t = True,
10470+
level: Level | None = None,
10471+
ddof: int = 1,
10472+
numeric_only: bool_t | None = None,
10473+
**kwargs,
10474+
) -> Series | float:
1043310475
return self._stat_function_ddof(
1043410476
"sem", nanops.nansem, axis, skipna, level, ddof, numeric_only, **kwargs
1043510477
)
1043610478

1043710479
def var(
10438-
self, axis=None, skipna=True, level=None, ddof=1, numeric_only=None, **kwargs
10439-
):
10480+
self,
10481+
axis: Axis | None = None,
10482+
skipna: bool_t = True,
10483+
level: Level | None = None,
10484+
ddof: int = 1,
10485+
numeric_only: bool_t | None = None,
10486+
**kwargs,
10487+
) -> Series | float:
1044010488
return self._stat_function_ddof(
1044110489
"var", nanops.nanvar, axis, skipna, level, ddof, numeric_only, **kwargs
1044210490
)
1044310491

1044410492
def std(
10445-
self, axis=None, skipna=True, level=None, ddof=1, numeric_only=None, **kwargs
10446-
):
10493+
self,
10494+
axis: Axis | None = None,
10495+
skipna: bool_t = True,
10496+
level: Level | None = None,
10497+
ddof: int = 1,
10498+
numeric_only: bool_t | None = None,
10499+
**kwargs,
10500+
) -> Series | float:
1044710501
return self._stat_function_ddof(
1044810502
"std", nanops.nanstd, axis, skipna, level, ddof, numeric_only, **kwargs
1044910503
)
@@ -10453,10 +10507,10 @@ def _stat_function(
1045310507
self,
1045410508
name: str,
1045510509
func,
10456-
axis=None,
10457-
skipna=True,
10458-
level=None,
10459-
numeric_only=None,
10510+
axis: Axis | None = None,
10511+
skipna: bool_t = True,
10512+
level: Level | None = None,
10513+
numeric_only: bool_t | None = None,
1046010514
**kwargs,
1046110515
):
1046210516
if name == "median":
@@ -10483,32 +10537,74 @@ def _stat_function(
1048310537
func, name=name, axis=axis, skipna=skipna, numeric_only=numeric_only
1048410538
)
1048510539

10486-
def min(self, axis=None, skipna=True, level=None, numeric_only=None, **kwargs):
10540+
def min(
10541+
self,
10542+
axis: Axis | None = None,
10543+
skipna: bool_t = True,
10544+
level: Level | None = None,
10545+
numeric_only: bool_t | None = None,
10546+
**kwargs,
10547+
):
1048710548
return self._stat_function(
1048810549
"min", nanops.nanmin, axis, skipna, level, numeric_only, **kwargs
1048910550
)
1049010551

10491-
def max(self, axis=None, skipna=True, level=None, numeric_only=None, **kwargs):
10552+
def max(
10553+
self,
10554+
axis: Axis | None = None,
10555+
skipna: bool_t = True,
10556+
level: Level | None = None,
10557+
numeric_only: bool_t | None = None,
10558+
**kwargs,
10559+
):
1049210560
return self._stat_function(
1049310561
"max", nanops.nanmax, axis, skipna, level, numeric_only, **kwargs
1049410562
)
1049510563

10496-
def mean(self, axis=None, skipna=True, level=None, numeric_only=None, **kwargs):
10564+
def mean(
10565+
self,
10566+
axis: Axis | None = None,
10567+
skipna: bool_t = True,
10568+
level: Level | None = None,
10569+
numeric_only: bool_t | None = None,
10570+
**kwargs,
10571+
) -> Series | float:
1049710572
return self._stat_function(
1049810573
"mean", nanops.nanmean, axis, skipna, level, numeric_only, **kwargs
1049910574
)
1050010575

10501-
def median(self, axis=None, skipna=True, level=None, numeric_only=None, **kwargs):
10576+
def median(
10577+
self,
10578+
axis: Axis | None = None,
10579+
skipna: bool_t = True,
10580+
level: Level | None = None,
10581+
numeric_only: bool_t | None = None,
10582+
**kwargs,
10583+
) -> Series | float:
1050210584
return self._stat_function(
1050310585
"median", nanops.nanmedian, axis, skipna, level, numeric_only, **kwargs
1050410586
)
1050510587

10506-
def skew(self, axis=None, skipna=True, level=None, numeric_only=None, **kwargs):
10588+
def skew(
10589+
self,
10590+
axis: Axis | None = None,
10591+
skipna: bool_t = True,
10592+
level: Level | None = None,
10593+
numeric_only: bool_t | None = None,
10594+
**kwargs,
10595+
) -> Series | float:
1050710596
return self._stat_function(
1050810597
"skew", nanops.nanskew, axis, skipna, level, numeric_only, **kwargs
1050910598
)
1051010599

10511-
def kurt(self, axis=None, skipna=True, level=None, numeric_only=None, **kwargs):
10600+
def kurt(
10601+
self,
10602+
axis: Axis | None = None,
10603+
skipna: bool_t = True,
10604+
level: Level | None = None,
10605+
numeric_only: bool_t | None = None,
10606+
**kwargs,
10607+
) -> Series | float:
1051210608
return self._stat_function(
1051310609
"kurt", nanops.nankurt, axis, skipna, level, numeric_only, **kwargs
1051410610
)
@@ -10520,11 +10616,11 @@ def _min_count_stat_function(
1052010616
self,
1052110617
name: str,
1052210618
func,
10523-
axis=None,
10524-
skipna=True,
10525-
level=None,
10526-
numeric_only=None,
10527-
min_count=0,
10619+
axis: Axis | None = None,
10620+
skipna: bool_t = True,
10621+
level: Level | None = None,
10622+
numeric_only: bool_t | None = None,
10623+
min_count: int = 0,
1052810624
**kwargs,
1052910625
):
1053010626
if name == "sum":
@@ -10565,10 +10661,10 @@ def _min_count_stat_function(
1056510661

1056610662
def sum(
1056710663
self,
10568-
axis=None,
10569-
skipna=True,
10570-
level=None,
10571-
numeric_only=None,
10664+
axis: Axis | None = None,
10665+
skipna: bool_t = True,
10666+
level: Level | None = None,
10667+
numeric_only: bool_t | None = None,
1057210668
min_count=0,
1057310669
**kwargs,
1057410670
):
@@ -10578,11 +10674,11 @@ def sum(
1057810674

1057910675
def prod(
1058010676
self,
10581-
axis=None,
10582-
skipna=True,
10583-
level=None,
10584-
numeric_only=None,
10585-
min_count=0,
10677+
axis: Axis | None = None,
10678+
skipna: bool_t = True,
10679+
level: Level | None = None,
10680+
numeric_only: bool_t | None = None,
10681+
min_count: int = 0,
1058610682
**kwargs,
1058710683
):
1058810684
return self._min_count_stat_function(
@@ -10598,7 +10694,12 @@ def prod(
1059810694

1059910695
product = prod
1060010696

10601-
def mad(self, axis=None, skipna=True, level=None):
10697+
def mad(
10698+
self,
10699+
axis: Axis | None = None,
10700+
skipna: bool_t = True,
10701+
level: Level | None = None,
10702+
) -> Series | float:
1060210703
"""
1060310704
{desc}
1060410705

pandas/core/groupby/groupby.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -1931,7 +1931,7 @@ def mean(
19311931
2 4.0
19321932
Name: B, dtype: float64
19331933
"""
1934-
numeric_only = self._resolve_numeric_only(numeric_only)
1934+
numeric_only_bool = self._resolve_numeric_only(numeric_only)
19351935

19361936
if maybe_use_numba(engine):
19371937
from pandas.core._numba.kernels import sliding_mean
@@ -1940,8 +1940,8 @@ def mean(
19401940
else:
19411941
result = self._cython_agg_general(
19421942
"mean",
1943-
alt=lambda x: Series(x).mean(numeric_only=numeric_only),
1944-
numeric_only=numeric_only,
1943+
alt=lambda x: Series(x).mean(numeric_only=numeric_only_bool),
1944+
numeric_only=numeric_only_bool,
19451945
)
19461946
return result.__finalize__(self.obj, method="groupby")
19471947

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

19701970
result = self._cython_agg_general(
19711971
"median",
1972-
alt=lambda x: Series(x).median(numeric_only=numeric_only),
1973-
numeric_only=numeric_only,
1972+
alt=lambda x: Series(x).median(numeric_only=numeric_only_bool),
1973+
numeric_only=numeric_only_bool,
19741974
)
19751975
return result.__finalize__(self.obj, method="groupby")
19761976

0 commit comments

Comments
 (0)