@@ -4545,17 +4545,16 @@ def pipe(self, func, *args, **kwargs):
4545
4545
4546
4546
Parameters
4547
4547
----------
4548
- func : function, string, dictionary, or list of string/functions
4548
+ func : function, str, list or dict
4549
4549
Function to use for aggregating the data. If a function, must either
4550
- work when passed a %(klass)s or when passed to %(klass)s.apply. For
4551
- a DataFrame, can pass a dict, if the keys are DataFrame column names.
4550
+ work when passed a %(klass)s or when passed to %(klass)s.apply.
4552
4551
4553
4552
Accepted combinations are:
4554
4553
4555
- - string function name.
4556
- - function.
4557
- - list of functions.
4558
- - dict of column names -> functions ( or list of functions) .
4554
+ - function
4555
+ - string function name
4556
+ - list of functions and/or function names, e.g. ``[np.sum, 'mean']``
4557
+ - dict of axis labels -> functions, function names or list of such .
4559
4558
%(axis)s
4560
4559
*args
4561
4560
Positional arguments to pass to `func`.
@@ -4564,7 +4563,11 @@ def pipe(self, func, *args, **kwargs):
4564
4563
4565
4564
Returns
4566
4565
-------
4567
- aggregated : %(klass)s
4566
+ DataFrame, Series or scalar
4567
+ if DataFrame.agg is called with a single function, returns a Series
4568
+ if DataFrame.agg is called with several functions, returns a DataFrame
4569
+ if Series.agg is called with single function, returns a scalar
4570
+ if Series.agg is called with several functions, returns a Series
4568
4571
4569
4572
Notes
4570
4573
-----
@@ -4574,50 +4577,71 @@ def pipe(self, func, *args, **kwargs):
4574
4577
""" )
4575
4578
4576
4579
_shared_docs ['transform' ] = ("""
4577
- Call function producing a like-indexed %(klass)s
4578
- and return a %(klass)s with the transformed values
4580
+ Call ``func`` on self producing a %(klass)s with transformed values
4581
+ and that has the same axis length as self.
4579
4582
4580
4583
.. versionadded:: 0.20.0
4581
4584
4582
4585
Parameters
4583
4586
----------
4584
- func : callable, string, dictionary, or list of string/callables
4585
- To apply to column
4587
+ func : function, str, list or dict
4588
+ Function to use for transforming the data. If a function, must either
4589
+ work when passed a %(klass)s or when passed to %(klass)s.apply.
4586
4590
4587
- Accepted Combinations are:
4591
+ Accepted combinations are:
4588
4592
4589
- - string function name
4590
4593
- function
4591
- - list of functions
4592
- - dict of column names -> functions (or list of functions)
4594
+ - string function name
4595
+ - list of functions and/or function names, e.g. ``[np.exp. 'sqrt']``
4596
+ - dict of axis labels -> functions, function names or list of such.
4597
+ %(axis)s
4598
+ *args
4599
+ Positional arguments to pass to `func`.
4600
+ **kwargs
4601
+ Keyword arguments to pass to `func`.
4593
4602
4594
4603
Returns
4595
4604
-------
4596
- transformed : %(klass)s
4605
+ %(klass)s
4606
+ A %(klass)s that must have the same length as self.
4597
4607
4598
- Examples
4608
+ Raises
4609
+ ------
4610
+ ValueError : If the returned %(klass)s has a different length than self.
4611
+
4612
+ See Also
4599
4613
--------
4600
- >>> df = pd.DataFrame(np.random.randn(10, 3), columns=['A', 'B', 'C'],
4601
- ... index=pd.date_range('1/1/2000', periods=10))
4602
- df.iloc[3:7] = np.nan
4603
-
4604
- >>> df.transform(lambda x: (x - x.mean()) / x.std())
4605
- A B C
4606
- 2000-01-01 0.579457 1.236184 0.123424
4607
- 2000-01-02 0.370357 -0.605875 -1.231325
4608
- 2000-01-03 1.455756 -0.277446 0.288967
4609
- 2000-01-04 NaN NaN NaN
4610
- 2000-01-05 NaN NaN NaN
4611
- 2000-01-06 NaN NaN NaN
4612
- 2000-01-07 NaN NaN NaN
4613
- 2000-01-08 -0.498658 1.274522 1.642524
4614
- 2000-01-09 -0.540524 -1.012676 -0.828968
4615
- 2000-01-10 -1.366388 -0.614710 0.005378
4616
-
4617
- See also
4614
+ %(klass)s.agg : Only perform aggregating type operations.
4615
+ %(klass)s.apply : Invoke function on a %(klass)s.
4616
+
4617
+ Examples
4618
4618
--------
4619
- pandas.%(klass)s.aggregate
4620
- pandas.%(klass)s.apply
4619
+ >>> df = pd.DataFrame({'A': range(3), 'B': range(1, 4)})
4620
+ >>> df
4621
+ A B
4622
+ 0 0 1
4623
+ 1 1 2
4624
+ 2 2 3
4625
+ >>> df.transform(lambda x: x + 1)
4626
+ A B
4627
+ 0 1 2
4628
+ 1 2 3
4629
+ 2 3 4
4630
+
4631
+ Even though the resulting %(klass)s must have the same length as the
4632
+ input %(klass)s, it is possible to provide several input functions:
4633
+
4634
+ >>> s = pd.Series(range(3))
4635
+ >>> s
4636
+ 0 0
4637
+ 1 1
4638
+ 2 2
4639
+ dtype: int64
4640
+ >>> s.transform([np.sqrt, np.exp])
4641
+ sqrt exp
4642
+ 0 0.000000 1.000000
4643
+ 1 1.000000 2.718282
4644
+ 2 1.414214 7.389056
4621
4645
""" )
4622
4646
4623
4647
# ----------------------------------------------------------------------
@@ -9401,7 +9425,7 @@ def ewm(self, com=None, span=None, halflife=None, alpha=None,
9401
9425
9402
9426
cls .ewm = ewm
9403
9427
9404
- @Appender (_shared_docs ['transform' ] % _shared_doc_kwargs )
9428
+ @Appender (_shared_docs ['transform' ] % dict ( axis = "" , ** _shared_doc_kwargs ) )
9405
9429
def transform (self , func , * args , ** kwargs ):
9406
9430
result = self .agg (func , * args , ** kwargs )
9407
9431
if is_scalar (result ) or len (result ) != len (self ):
0 commit comments