Skip to content

Commit 207abd6

Browse files
committed
docs: updating docs to better suit dataframes and series
1 parent f6c24f7 commit 207abd6

File tree

2 files changed

+63
-31
lines changed

2 files changed

+63
-31
lines changed

pandas/core/groupby/generic.py

+60-2
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,32 @@ def _aggregate_named(self, func, *args, **kwargs):
429429

430430
return result
431431

432-
@Substitution(klass="Series")
432+
__examples_series_doc = dedent(
433+
"""
434+
ser = pd.Series(
435+
... [390.0, 350.0, 30.0, 20.0],
436+
... index=["Falcon", "Falcon", "Parrot", "Parrot"],
437+
... name="Max Speed")
438+
>>> grouped = ser.groupby([1, 1, 2, 2])
439+
>>> grouped.transform(lambda x: (x - x.mean()) / x.std())
440+
Falcon 0.707107
441+
Falcon -0.707107
442+
Parrot 0.707107
443+
Parrot -0.707107
444+
Name: Max Speed, dtype: float64
445+
446+
Broadcast result of the transformation
447+
448+
>>> grouped.transform(lambda x: x.max() - x.min())
449+
Falcon 40.0
450+
Falcon 40.0
451+
Parrot 10.0
452+
Parrot 10.0
453+
Name: Max Speed, dtype: float64
454+
"""
455+
)
456+
457+
@Substitution(klass="Series", example_class="ser", example=__examples_series_doc)
433458
@Appender(_transform_template)
434459
def transform(self, func, *args, engine=None, engine_kwargs=None, **kwargs):
435460
return self._transform(
@@ -1425,7 +1450,40 @@ def _transform_general(self, func, *args, **kwargs):
14251450
concatenated = concatenated.reindex(concat_index, axis=other_axis, copy=False)
14261451
return self._set_result_index_ordered(concatenated)
14271452

1428-
@Substitution(klass="DataFrame")
1453+
__examples_dataframe_doc = dedent(
1454+
"""
1455+
df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar',
1456+
... 'foo', 'bar'],
1457+
... 'B' : ['one', 'one', 'two', 'three',
1458+
... 'two', 'two'],
1459+
... 'C' : [1, 5, 5, 2, 5, 5],
1460+
... 'D' : [2.0, 5., 8., 1., 2., 9.]})
1461+
>>> grouped = df.groupby('A')[['C', 'D']]
1462+
>>> grouped.transform(lambda x: (x - x.mean()) / x.std())
1463+
C D
1464+
0 -1.154701 -0.577350
1465+
1 0.577350 0.000000
1466+
2 0.577350 1.154701
1467+
3 -1.154701 -1.000000
1468+
4 0.577350 -0.577350
1469+
5 0.577350 1.000000
1470+
1471+
Broadcast result of the transformation
1472+
1473+
>>> grouped.transform(lambda x: x.max() - x.min())
1474+
C D
1475+
0 4.0 6.0
1476+
1 3.0 8.0
1477+
2 4.0 6.0
1478+
3 3.0 8.0
1479+
4 4.0 6.0
1480+
5 3.0 8.0
1481+
"""
1482+
)
1483+
1484+
@Substitution(
1485+
klass="DataFrame", example_class="df", example=__examples_dataframe_doc
1486+
)
14291487
@Appender(_transform_template)
14301488
def transform(self, func, *args, engine=None, engine_kwargs=None, **kwargs):
14311489
return self._transform(

pandas/core/groupby/groupby.py

+3-29
Original file line numberDiff line numberDiff line change
@@ -408,16 +408,15 @@ class providing the base-class of operations.
408408
- string function name
409409
- Numba JIT function with ``engine='numba'`` specified.
410410
411+
Only passing a single function is supported with this engine.
411412
If the ``'numba'`` engine is chosen, the function must be
412413
a user defined function with ``values`` and ``index`` as the
413414
first and second arguments respectively in the function signature.
414415
Each group's index will be passed to the user defined function
415416
and optionally available for use.
416417
417418
If a string is chosen, then it needs to be the string of the
418-
function you want to use.
419-
420-
IE: ``pd.%(klass)s = grouped.transform("sum")``.
419+
groupby method you want to use.
421420
422421
.. versionchanged:: 1.1.0
423422
*args
@@ -490,32 +489,7 @@ class providing the base-class of operations.
490489
Examples
491490
--------
492491
493-
>>> df = pd.%(klass)s({'A' : ['foo', 'bar', 'foo', 'bar',
494-
... 'foo', 'bar'],
495-
... 'B' : ['one', 'one', 'two', 'three',
496-
... 'two', 'two'],
497-
... 'C' : [1, 5, 5, 2, 5, 5],
498-
... 'D' : [2.0, 5., 8., 1., 2., 9.]})
499-
>>> grouped = df.groupby('A')[['C', 'D']]
500-
>>> grouped.transform(lambda x: (x - x.mean()) / x.std())
501-
C D
502-
0 -1.154701 -0.577350
503-
1 0.577350 0.000000
504-
2 0.577350 1.154701
505-
3 -1.154701 -1.000000
506-
4 0.577350 -0.577350
507-
5 0.577350 1.000000
508-
509-
Broadcast result of the transformation
510-
511-
>>> grouped.transform(lambda x: x.max() - x.min())
512-
C D
513-
0 4.0 6.0
514-
1 3.0 8.0
515-
2 4.0 6.0
516-
3 3.0 8.0
517-
4 4.0 6.0
518-
5 3.0 8.0
492+
>>> %(example)s
519493
520494
.. versionchanged:: 1.3.0
521495

0 commit comments

Comments
 (0)