Skip to content

Commit 8aa085d

Browse files
DOC: fix RT03, ES01 for pandas.core.groupby.SeriesGroupBy.agg
1 parent b948821 commit 8aa085d

File tree

2 files changed

+134
-4
lines changed

2 files changed

+134
-4
lines changed

ci/code_checks.sh

-2
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
140140
-i "pandas.core.groupby.DataFrameGroupBy.plot PR02" \
141141
-i "pandas.core.groupby.DataFrameGroupBy.sem SA01" \
142142
-i "pandas.core.groupby.SeriesGroupBy.__iter__ RT03,SA01" \
143-
-i "pandas.core.groupby.SeriesGroupBy.agg RT03" \
144-
-i "pandas.core.groupby.SeriesGroupBy.aggregate RT03" \
145143
-i "pandas.core.groupby.SeriesGroupBy.get_group RT03,SA01" \
146144
-i "pandas.core.groupby.SeriesGroupBy.groups SA01" \
147145
-i "pandas.core.groupby.SeriesGroupBy.indices SA01" \

pandas/core/groupby/generic.py

+134-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868
GroupBy,
6969
GroupByPlot,
7070
_agg_template_frame,
71-
_agg_template_series,
7271
_transform_template,
7372
)
7473
from pandas.core.indexes.api import (
@@ -324,8 +323,141 @@ def apply(self, func, *args, **kwargs) -> Series:
324323
"""
325324
return super().apply(func, *args, **kwargs)
326325

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

0 commit comments

Comments
 (0)