Skip to content

Commit d538a1c

Browse files
tuhinsharma121mroeschkerhshadrach
authored
DOC: fix RT03, ES01 for pandas.core.groupby.SeriesGroupBy.agg and pandas.core.groupby.SeriesGroupBy.aggregate (pandas-dev#59898)
* DOC: fix RT03, ES01 for pandas.core.groupby.SeriesGroupBy.agg * DOC: remove _agg_template_series Co-authored-by: mroeschke <[email protected]> Co-authored-by: rhshadrach <[email protected]> * DOC: remove _agg_template_series Co-authored-by: mroeschke <[email protected]> Co-authored-by: rhshadrach <[email protected]> * DOC: remove _agg_template_seris --------- Co-authored-by: mroeschke <[email protected]> Co-authored-by: rhshadrach <[email protected]>
1 parent cf12e67 commit d538a1c

File tree

4 files changed

+134
-86
lines changed

4 files changed

+134
-86
lines changed

ci/code_checks.sh

-2
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
137137
-i "pandas.core.groupby.DataFrameGroupBy.plot PR02" \
138138
-i "pandas.core.groupby.DataFrameGroupBy.sem SA01" \
139139
-i "pandas.core.groupby.SeriesGroupBy.__iter__ RT03,SA01" \
140-
-i "pandas.core.groupby.SeriesGroupBy.agg RT03" \
141-
-i "pandas.core.groupby.SeriesGroupBy.aggregate RT03" \
142140
-i "pandas.core.groupby.SeriesGroupBy.get_group RT03,SA01" \
143141
-i "pandas.core.groupby.SeriesGroupBy.groups SA01" \
144142
-i "pandas.core.groupby.SeriesGroupBy.indices SA01" \

pandas/core/groupby/generic.py

+134-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
6767
from pandas.core.groupby.groupby import (
6868
GroupBy,
6969
GroupByPlot,
70-
_agg_template_series,
7170
_transform_template,
7271
)
7372
from pandas.core.indexes.api import (
@@ -323,8 +322,141 @@ def apply(self, func, *args, **kwargs) -> Series:
323322
"""
324323
return super().apply(func, *args, **kwargs)
325324

326-
@doc(_agg_template_series, examples=_agg_examples_doc, klass="Series")
327325
def aggregate(self, func=None, *args, engine=None, engine_kwargs=None, **kwargs):
326+
"""
327+
Aggregate using one or more operations.
328+
329+
The ``aggregate`` method enables flexible and efficient aggregation of grouped
330+
data using a variety of functions, including built-in, user-defined, and
331+
optimized JIT-compiled functions.
332+
333+
Parameters
334+
----------
335+
func : function, str, list, dict or None
336+
Function to use for aggregating the data. If a function, must either
337+
work when passed a Series or when passed to Series.apply.
338+
339+
Accepted combinations are:
340+
341+
- function
342+
- string function name
343+
- list of functions and/or function names, e.g. ``[np.sum, 'mean']``
344+
- None, in which case ``**kwargs`` are used with Named Aggregation. Here
345+
the output has one column for each element in ``**kwargs``. The name of
346+
the column is keyword, whereas the value determines the aggregation
347+
used to compute the values in the column.
348+
349+
Can also accept a Numba JIT function with
350+
``engine='numba'`` specified. Only passing a single function is supported
351+
with this engine.
352+
353+
If the ``'numba'`` engine is chosen, the function must be
354+
a user defined function with ``values`` and ``index`` as the
355+
first and second arguments respectively in the function signature.
356+
Each group's index will be passed to the user defined function
357+
and optionally available for use.
358+
359+
.. deprecated:: 2.1.0
360+
361+
Passing a dictionary is deprecated and will raise in a future version
362+
of pandas. Pass a list of aggregations instead.
363+
*args
364+
Positional arguments to pass to func.
365+
engine : str, default None
366+
* ``'cython'`` : Runs the function through C-extensions from cython.
367+
* ``'numba'`` : Runs the function through JIT compiled code from numba.
368+
* ``None`` : Defaults to ``'cython'`` or globally setting
369+
``compute.use_numba``
370+
371+
engine_kwargs : dict, default None
372+
* For ``'cython'`` engine, there are no accepted ``engine_kwargs``
373+
* For ``'numba'`` engine, the engine can accept ``nopython``, ``nogil``
374+
and ``parallel`` dictionary keys. The values must either be ``True`` or
375+
``False``. The default ``engine_kwargs`` for the ``'numba'`` engine is
376+
``{'nopython': True, 'nogil': False, 'parallel': False}`` and will be
377+
applied to the function
378+
379+
**kwargs
380+
* If ``func`` is None, ``**kwargs`` are used to define the output names and
381+
aggregations via Named Aggregation. See ``func`` entry.
382+
* Otherwise, keyword arguments to be passed into func.
383+
384+
Returns
385+
-------
386+
Series
387+
Aggregated Series based on the grouping and the applied aggregation
388+
functions.
389+
390+
See Also
391+
--------
392+
SeriesGroupBy.apply : Apply function func group-wise
393+
and combine the results together.
394+
SeriesGroupBy.transform : Transforms the Series on each group
395+
based on the given function.
396+
Series.aggregate : Aggregate using one or more operations.
397+
398+
Notes
399+
-----
400+
When using ``engine='numba'``, there will be no "fall back" behavior internally.
401+
The group data and group index will be passed as numpy arrays to the JITed
402+
user defined function, and no alternative execution attempts will be tried.
403+
404+
Functions that mutate the passed object can produce unexpected
405+
behavior or errors and are not supported. See :ref:`gotchas.udf-mutation`
406+
for more details.
407+
408+
.. versionchanged:: 1.3.0
409+
410+
The resulting dtype will reflect the return value of the passed ``func``,
411+
see the examples below.
412+
413+
Examples
414+
--------
415+
>>> s = pd.Series([1, 2, 3, 4])
416+
417+
>>> s
418+
0 1
419+
1 2
420+
2 3
421+
3 4
422+
dtype: int64
423+
424+
>>> s.groupby([1, 1, 2, 2]).min()
425+
1 1
426+
2 3
427+
dtype: int64
428+
429+
>>> s.groupby([1, 1, 2, 2]).agg("min")
430+
1 1
431+
2 3
432+
dtype: int64
433+
434+
>>> s.groupby([1, 1, 2, 2]).agg(["min", "max"])
435+
min max
436+
1 1 2
437+
2 3 4
438+
439+
The output column names can be controlled by passing
440+
the desired column names and aggregations as keyword arguments.
441+
442+
>>> s.groupby([1, 1, 2, 2]).agg(
443+
... minimum="min",
444+
... maximum="max",
445+
... )
446+
minimum maximum
447+
1 1 2
448+
2 3 4
449+
450+
.. versionchanged:: 1.3.0
451+
452+
The resulting dtype will reflect the return value of the aggregating
453+
function.
454+
455+
>>> s.groupby([1, 1, 2, 2]).agg(lambda x: x.astype(float).min())
456+
1 1.0
457+
2 3.0
458+
dtype: float64
459+
"""
328460
relabeling = func is None
329461
columns = None
330462
if relabeling:

pandas/core/groupby/groupby.py

-81
Original file line numberDiff line numberDiff line change
@@ -364,87 +364,6 @@ class providing the base-class of operations.
364364
--------
365365
%(example)s"""
366366

367-
_agg_template_series = """
368-
Aggregate using one or more operations.
369-
370-
Parameters
371-
----------
372-
func : function, str, list, dict or None
373-
Function to use for aggregating the data. If a function, must either
374-
work when passed a {klass} or when passed to {klass}.apply.
375-
376-
Accepted combinations are:
377-
378-
- function
379-
- string function name
380-
- list of functions and/or function names, e.g. ``[np.sum, 'mean']``
381-
- None, in which case ``**kwargs`` are used with Named Aggregation. Here the
382-
output has one column for each element in ``**kwargs``. The name of the
383-
column is keyword, whereas the value determines the aggregation used to compute
384-
the values in the column.
385-
386-
Can also accept a Numba JIT function with
387-
``engine='numba'`` specified. Only passing a single function is supported
388-
with this engine.
389-
390-
If the ``'numba'`` engine is chosen, the function must be
391-
a user defined function with ``values`` and ``index`` as the
392-
first and second arguments respectively in the function signature.
393-
Each group's index will be passed to the user defined function
394-
and optionally available for use.
395-
396-
.. deprecated:: 2.1.0
397-
398-
Passing a dictionary is deprecated and will raise in a future version
399-
of pandas. Pass a list of aggregations instead.
400-
*args
401-
Positional arguments to pass to func.
402-
engine : str, default None
403-
* ``'cython'`` : Runs the function through C-extensions from cython.
404-
* ``'numba'`` : Runs the function through JIT compiled code from numba.
405-
* ``None`` : Defaults to ``'cython'`` or globally setting ``compute.use_numba``
406-
407-
engine_kwargs : dict, default None
408-
* For ``'cython'`` engine, there are no accepted ``engine_kwargs``
409-
* For ``'numba'`` engine, the engine can accept ``nopython``, ``nogil``
410-
and ``parallel`` dictionary keys. The values must either be ``True`` or
411-
``False``. The default ``engine_kwargs`` for the ``'numba'`` engine is
412-
``{{'nopython': True, 'nogil': False, 'parallel': False}}`` and will be
413-
applied to the function
414-
415-
**kwargs
416-
* If ``func`` is None, ``**kwargs`` are used to define the output names and
417-
aggregations via Named Aggregation. See ``func`` entry.
418-
* Otherwise, keyword arguments to be passed into func.
419-
420-
Returns
421-
-------
422-
{klass}
423-
424-
See Also
425-
--------
426-
{klass}GroupBy.apply : Apply function func group-wise
427-
and combine the results together.
428-
{klass}GroupBy.transform : Transforms the Series on each group
429-
based on the given function.
430-
{klass}.aggregate : Aggregate using one or more operations.
431-
432-
Notes
433-
-----
434-
When using ``engine='numba'``, there will be no "fall back" behavior internally.
435-
The group data and group index will be passed as numpy arrays to the JITed
436-
user defined function, and no alternative execution attempts will be tried.
437-
438-
Functions that mutate the passed object can produce unexpected
439-
behavior or errors and are not supported. See :ref:`gotchas.udf-mutation`
440-
for more details.
441-
442-
.. versionchanged:: 1.3.0
443-
444-
The resulting dtype will reflect the return value of the passed ``func``,
445-
see the examples below.
446-
{examples}"""
447-
448367

449368
@final
450369
class GroupByPlot(PandasObject):

scripts/validate_unwanted_patterns.py

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
"_shared_docs",
3030
"_new_Index",
3131
"_new_PeriodIndex",
32-
"_agg_template_series",
3332
"_pipe_template",
3433
"_apply_groupings_depr",
3534
"__main__",

0 commit comments

Comments
 (0)