Skip to content

Commit 098a569

Browse files
authored
Add type literals to engine argument in groupby aggregations (#54141)
1 parent 9f240a2 commit 098a569

File tree

1 file changed

+55
-9
lines changed

1 file changed

+55
-9
lines changed

pandas/core/groupby/groupby.py

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,46 @@ class providing the base-class of operations.
351351
{example}
352352
"""
353353

354+
_groupby_agg_method_engine_template = """
355+
Compute {fname} of group values.
356+
357+
Parameters
358+
----------
359+
numeric_only : bool, default {no}
360+
Include only float, int, boolean columns.
361+
362+
.. versionchanged:: 2.0.0
363+
364+
numeric_only no longer accepts ``None``.
365+
366+
min_count : int, default {mc}
367+
The required number of valid values to perform the operation. If fewer
368+
than ``min_count`` non-NA values are present the result will be NA.
369+
370+
engine : str, default None {e}
371+
* ``'cython'`` : Runs rolling apply through C-extensions from cython.
372+
* ``'numba'`` : Runs rolling apply through JIT compiled code from numba.
373+
Only available when ``raw`` is set to ``True``.
374+
* ``None`` : Defaults to ``'cython'`` or globally setting ``compute.use_numba``
375+
376+
engine_kwargs : dict, default None {ek}
377+
* For ``'cython'`` engine, there are no accepted ``engine_kwargs``
378+
* For ``'numba'`` engine, the engine can accept ``nopython``, ``nogil``
379+
and ``parallel`` dictionary keys. The values must either be ``True`` or
380+
``False``. The default ``engine_kwargs`` for the ``'numba'`` engine is
381+
``{{'nopython': True, 'nogil': False, 'parallel': False}}`` and will be
382+
applied to both the ``func`` and the ``apply`` groupby aggregation.
383+
384+
Returns
385+
-------
386+
Series or DataFrame
387+
Computed {fname} of values within each group.
388+
389+
Examples
390+
--------
391+
{example}
392+
"""
393+
354394
_pipe_template = """
355395
Apply a ``func`` with arguments to this %(klass)s object and return its result.
356396
@@ -2231,7 +2271,7 @@ def hfunc(bvalues: ArrayLike) -> ArrayLike:
22312271
def mean(
22322272
self,
22332273
numeric_only: bool = False,
2234-
engine: str = "cython",
2274+
engine: Literal["cython", "numba"] | None = None,
22352275
engine_kwargs: dict[str, bool] | None = None,
22362276
):
22372277
"""
@@ -2402,7 +2442,7 @@ def median(self, numeric_only: bool = False):
24022442
def std(
24032443
self,
24042444
ddof: int = 1,
2405-
engine: str | None = None,
2445+
engine: Literal["cython", "numba"] | None = None,
24062446
engine_kwargs: dict[str, bool] | None = None,
24072447
numeric_only: bool = False,
24082448
):
@@ -2511,7 +2551,7 @@ def std(
25112551
def var(
25122552
self,
25132553
ddof: int = 1,
2514-
engine: str | None = None,
2554+
engine: Literal["cython", "numba"] | None = None,
25152555
engine_kwargs: dict[str, bool] | None = None,
25162556
numeric_only: bool = False,
25172557
):
@@ -2909,10 +2949,12 @@ def size(self) -> DataFrame | Series:
29092949

29102950
@final
29112951
@doc(
2912-
_groupby_agg_method_template,
2952+
_groupby_agg_method_engine_template,
29132953
fname="sum",
29142954
no=False,
29152955
mc=0,
2956+
e=None,
2957+
ek=None,
29162958
example=dedent(
29172959
"""\
29182960
For SeriesGroupBy:
@@ -2952,7 +2994,7 @@ def sum(
29522994
self,
29532995
numeric_only: bool = False,
29542996
min_count: int = 0,
2955-
engine: str | None = None,
2997+
engine: Literal["cython", "numba"] | None = None,
29562998
engine_kwargs: dict[str, bool] | None = None,
29572999
):
29583000
if maybe_use_numba(engine):
@@ -3026,10 +3068,12 @@ def prod(self, numeric_only: bool = False, min_count: int = 0):
30263068

30273069
@final
30283070
@doc(
3029-
_groupby_agg_method_template,
3071+
_groupby_agg_method_engine_template,
30303072
fname="min",
30313073
no=False,
30323074
mc=-1,
3075+
e=None,
3076+
ek=None,
30333077
example=dedent(
30343078
"""\
30353079
For SeriesGroupBy:
@@ -3069,7 +3113,7 @@ def min(
30693113
self,
30703114
numeric_only: bool = False,
30713115
min_count: int = -1,
3072-
engine: str | None = None,
3116+
engine: Literal["cython", "numba"] | None = None,
30733117
engine_kwargs: dict[str, bool] | None = None,
30743118
):
30753119
if maybe_use_numba(engine):
@@ -3092,10 +3136,12 @@ def min(
30923136

30933137
@final
30943138
@doc(
3095-
_groupby_agg_method_template,
3139+
_groupby_agg_method_engine_template,
30963140
fname="max",
30973141
no=False,
30983142
mc=-1,
3143+
e=None,
3144+
ek=None,
30993145
example=dedent(
31003146
"""\
31013147
For SeriesGroupBy:
@@ -3135,7 +3181,7 @@ def max(
31353181
self,
31363182
numeric_only: bool = False,
31373183
min_count: int = -1,
3138-
engine: str | None = None,
3184+
engine: Literal["cython", "numba"] | None = None,
31393185
engine_kwargs: dict[str, bool] | None = None,
31403186
):
31413187
if maybe_use_numba(engine):

0 commit comments

Comments
 (0)