@@ -429,7 +429,32 @@ def _aggregate_named(self, func, *args, **kwargs):
429
429
430
430
return result
431
431
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 )
433
458
@Appender (_transform_template )
434
459
def transform (self , func , * args , engine = None , engine_kwargs = None , ** kwargs ):
435
460
return self ._transform (
@@ -1425,7 +1450,40 @@ def _transform_general(self, func, *args, **kwargs):
1425
1450
concatenated = concatenated .reindex (concat_index , axis = other_axis , copy = False )
1426
1451
return self ._set_result_index_ordered (concatenated )
1427
1452
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
+ )
1429
1487
@Appender (_transform_template )
1430
1488
def transform (self , func , * args , engine = None , engine_kwargs = None , ** kwargs ):
1431
1489
return self ._transform (
0 commit comments