Skip to content

Commit 90d8de2

Browse files
mroeschkeMatt Roeschke
and
Matt Roeschke
authored
DOC: Fix groupby.agg/transform rst reference and numba references (#33860)
* DOC: Fix groupby.agg/transform rst reference and numba references * maybe fix docstring validation * fix more warnings * Hopefully fix docstring validation Co-authored-by: Matt Roeschke <[email protected]>
1 parent c00293d commit 90d8de2

File tree

3 files changed

+87
-46
lines changed

3 files changed

+87
-46
lines changed

doc/source/reference/groupby.rst

+4-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ Function application
3636

3737
GroupBy.apply
3838
GroupBy.agg
39-
GroupBy.aggregate
40-
GroupBy.transform
39+
SeriesGroupBy.aggregate
40+
DataFrameGroupBy.aggregate
41+
SeriesGroupBy.transform
42+
DataFrameGroupBy.transform
4143
GroupBy.pipe
4244

4345
Computations / descriptive stats

pandas/core/groupby/generic.py

+10-40
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,11 @@
6363
import pandas.core.common as com
6464
from pandas.core.construction import create_series_with_explicit_dtype
6565
from pandas.core.frame import DataFrame
66-
from pandas.core.generic import ABCDataFrame, ABCSeries, NDFrame, _shared_docs
66+
from pandas.core.generic import ABCDataFrame, ABCSeries, NDFrame
6767
from pandas.core.groupby import base
6868
from pandas.core.groupby.groupby import (
6969
GroupBy,
70+
_agg_template,
7071
_apply_docs,
7172
_transform_template,
7273
get_groupby,
@@ -177,16 +178,6 @@ def _selection_name(self):
177178
else:
178179
return self._selection
179180

180-
_agg_see_also_doc = dedent(
181-
"""
182-
See Also
183-
--------
184-
pandas.Series.groupby.apply
185-
pandas.Series.groupby.transform
186-
pandas.Series.aggregate
187-
"""
188-
)
189-
190181
_agg_examples_doc = dedent(
191182
"""
192183
Examples
@@ -224,8 +215,7 @@ def _selection_name(self):
224215
... )
225216
minimum maximum
226217
1 1 2
227-
2 3 4
228-
"""
218+
2 3 4"""
229219
)
230220

231221
@Appender(
@@ -237,13 +227,9 @@ def apply(self, func, *args, **kwargs):
237227
return super().apply(func, *args, **kwargs)
238228

239229
@Substitution(
240-
see_also=_agg_see_also_doc,
241-
examples=_agg_examples_doc,
242-
versionadded="",
243-
klass="Series",
244-
axis="",
230+
examples=_agg_examples_doc, klass="Series",
245231
)
246-
@Appender(_shared_docs["aggregate"])
232+
@Appender(_agg_template)
247233
def aggregate(
248234
self, func=None, *args, engine="cython", engine_kwargs=None, **kwargs
249235
):
@@ -476,7 +462,7 @@ def _aggregate_named(self, func, *args, **kwargs):
476462

477463
return result
478464

479-
@Substitution(klass="Series", selected="A.")
465+
@Substitution(klass="Series")
480466
@Appender(_transform_template)
481467
def transform(self, func, *args, engine="cython", engine_kwargs=None, **kwargs):
482468
func = self._get_cython_func(func) or func
@@ -854,16 +840,6 @@ class DataFrameGroupBy(GroupBy[DataFrame]):
854840

855841
_apply_whitelist = base.dataframe_apply_whitelist
856842

857-
_agg_see_also_doc = dedent(
858-
"""
859-
See Also
860-
--------
861-
pandas.DataFrame.groupby.apply
862-
pandas.DataFrame.groupby.transform
863-
pandas.DataFrame.aggregate
864-
"""
865-
)
866-
867843
_agg_examples_doc = dedent(
868844
"""
869845
Examples
@@ -928,26 +904,20 @@ class DataFrameGroupBy(GroupBy[DataFrame]):
928904
1 1 0.590715
929905
2 3 0.704907
930906
931-
932907
- The keywords are the *output* column names
933908
- The values are tuples whose first element is the column to select
934909
and the second element is the aggregation to apply to that column.
935910
Pandas provides the ``pandas.NamedAgg`` namedtuple with the fields
936911
``['column', 'aggfunc']`` to make it clearer what the arguments are.
937912
As usual, the aggregation can be a callable or a string alias.
938913
939-
See :ref:`groupby.aggregate.named` for more.
940-
"""
914+
See :ref:`groupby.aggregate.named` for more."""
941915
)
942916

943917
@Substitution(
944-
see_also=_agg_see_also_doc,
945-
examples=_agg_examples_doc,
946-
versionadded="",
947-
klass="DataFrame",
948-
axis="",
918+
examples=_agg_examples_doc, klass="DataFrame",
949919
)
950-
@Appender(_shared_docs["aggregate"])
920+
@Appender(_agg_template)
951921
def aggregate(
952922
self, func=None, *args, engine="cython", engine_kwargs=None, **kwargs
953923
):
@@ -1467,7 +1437,7 @@ def _transform_general(
14671437
concatenated = concatenated.reindex(concat_index, axis=other_axis, copy=False)
14681438
return self._set_result_index_ordered(concatenated)
14691439

1470-
@Substitution(klass="DataFrame", selected="")
1440+
@Substitution(klass="DataFrame")
14711441
@Appender(_transform_template)
14721442
def transform(self, func, *args, engine="cython", engine_kwargs=None, **kwargs):
14731443

pandas/core/groupby/groupby.py

+73-4
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,9 @@ class providing the base-class of operations.
291291
292292
See Also
293293
--------
294-
aggregate, transform
294+
%(klass)s.groupby.apply
295+
%(klass)s.groupby.aggregate
296+
%(klass)s.transform
295297
296298
Notes
297299
-----
@@ -310,14 +312,17 @@ class providing the base-class of operations.
310312
* f must not mutate groups. Mutation is not supported and may
311313
produce unexpected results.
312314
315+
When using ``engine='numba'``, there will be no "fall back" behavior internally.
316+
The group data and group index will be passed as numpy arrays to the JITed
317+
user defined function, and no alternative execution attempts will be tried.
318+
313319
Examples
314320
--------
315321
316-
# Same shape
317322
>>> df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar',
318323
... 'foo', 'bar'],
319324
... 'B' : ['one', 'one', 'two', 'three',
320-
... 'two', 'two'],
325+
... 'two', 'two'],
321326
... 'C' : [1, 5, 5, 2, 5, 5],
322327
... 'D' : [2.0, 5., 8., 1., 2., 9.]})
323328
>>> grouped = df.groupby('A')
@@ -330,7 +335,8 @@ class providing the base-class of operations.
330335
4 0.577350 -0.577350
331336
5 0.577350 1.000000
332337
333-
# Broadcastable
338+
Broadcast result of the transformation
339+
334340
>>> grouped.transform(lambda x: x.max() - x.min())
335341
C D
336342
0 4 6.0
@@ -341,6 +347,69 @@ class providing the base-class of operations.
341347
5 3 8.0
342348
"""
343349

350+
_agg_template = """
351+
Aggregate using one or more operations over the specified axis.
352+
353+
Parameters
354+
----------
355+
func : function, str, list or dict
356+
Function to use for aggregating the data. If a function, must either
357+
work when passed a %(klass)s or when passed to %(klass)s.apply.
358+
359+
Accepted combinations are:
360+
361+
- function
362+
- string function name
363+
- list of functions and/or function names, e.g. ``[np.sum, 'mean']``
364+
- dict of axis labels -> functions, function names or list of such.
365+
366+
Can also accept a Numba JIT function with
367+
``engine='numba'`` specified.
368+
369+
If the ``'numba'`` engine is chosen, the function must be
370+
a user defined function with ``values`` and ``index`` as the
371+
first and second arguments respectively in the function signature.
372+
Each group's index will be passed to the user defined function
373+
and optionally available for use.
374+
375+
.. versionchanged:: 1.1.0
376+
*args
377+
Positional arguments to pass to func
378+
engine : str, default 'cython'
379+
* ``'cython'`` : Runs the function through C-extensions from cython.
380+
* ``'numba'`` : Runs the function through JIT compiled code from numba.
381+
382+
.. versionadded:: 1.1.0
383+
engine_kwargs : dict, default None
384+
* For ``'cython'`` engine, there are no accepted ``engine_kwargs``
385+
* For ``'numba'`` engine, the engine can accept ``nopython``, ``nogil``
386+
and ``parallel`` dictionary keys. The values must either be ``True`` or
387+
``False``. The default ``engine_kwargs`` for the ``'numba'`` engine is
388+
``{'nopython': True, 'nogil': False, 'parallel': False}`` and will be
389+
applied to the function
390+
391+
.. versionadded:: 1.1.0
392+
**kwargs
393+
Keyword arguments to be passed into func.
394+
395+
Returns
396+
-------
397+
%(klass)s
398+
399+
See Also
400+
--------
401+
%(klass)s.groupby.apply
402+
%(klass)s.groupby.transform
403+
%(klass)s.aggregate
404+
405+
Notes
406+
-----
407+
When using ``engine='numba'``, there will be no "fall back" behavior internally.
408+
The group data and group index will be passed as numpy arrays to the JITed
409+
user defined function, and no alternative execution attempts will be tried.
410+
%(examples)s
411+
"""
412+
344413

345414
class GroupByPlot(PandasObject):
346415
"""

0 commit comments

Comments
 (0)