Skip to content

Commit 23609c3

Browse files
committed
improve doc string for df.aggregate and df.transform
1 parent 6da5a72 commit 23609c3

File tree

3 files changed

+84
-36
lines changed

3 files changed

+84
-36
lines changed

pandas/core/frame.py

+27
Original file line numberDiff line numberDiff line change
@@ -5995,6 +5995,33 @@ def _aggregate(self, arg, axis=0, *args, **kwargs):
59955995

59965996
agg = aggregate
59975997

5998+
_transform_doc = dedent("""
5999+
Examples
6000+
--------
6001+
>>> df = pd.DataFrame(np.random.randn(10, 2), columns=['A', 'B'],
6002+
... index=pd.date_range('1/1/2000', periods=10))
6003+
>>> df.iloc[3:7] = np.nan
6004+
6005+
>>> df.transform(lambda x: (x - x.mean()) / x.std())
6006+
A B
6007+
2000-01-01 0.579457 1.236184
6008+
2000-01-02 0.370357 -0.605875
6009+
2000-01-03 1.455756 -0.277446
6010+
2000-01-04 NaN NaN
6011+
2000-01-05 NaN NaN
6012+
2000-01-06 NaN NaN
6013+
2000-01-07 NaN NaN
6014+
2000-01-08 -0.498658 1.274522
6015+
2000-01-09 -0.540524 -1.012676
6016+
2000-01-10 -1.366388 -0.614710
6017+
6018+
See also
6019+
--------
6020+
pandas.DataFrame.aggregate
6021+
pandas.DataFrame.apply
6022+
""")
6023+
6024+
@Appender(_transform_doc)
59986025
@Appender(_shared_docs['transform'] % _shared_doc_kwargs)
59996026
def transform(self, func, axis=0, *args, **kwargs):
60006027
axis = self._get_axis_number(axis)

pandas/core/generic.py

+24-36
Original file line numberDiff line numberDiff line change
@@ -4545,17 +4545,16 @@ def pipe(self, func, *args, **kwargs):
45454545
45464546
Parameters
45474547
----------
4548-
func : function, string, dictionary, or list of string/functions
4548+
func : function, string, list of string/functions or dictionary
45494549
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.
45524551
45534552
Accepted combinations are:
45544553
4555-
- string function name.
4556-
- function.
4557-
- list of functions.
4558-
- dict of column names -> functions (or list of functions).
4554+
- string function name
4555+
- function
4556+
- list of functions and/or function names
4557+
- dict of axis labels -> functions, function names or list of such
45594558
%(axis)s
45604559
*args
45614560
Positional arguments to pass to `func`.
@@ -4581,43 +4580,32 @@ def pipe(self, func, *args, **kwargs):
45814580
45824581
Parameters
45834582
----------
4584-
func : callable, string, dictionary, or list of string/callables
4585-
To apply to column
4583+
func : function, string, list of string/functions or dictionary
4584+
Function to use for transforming the data. If a function, must either
4585+
work when passed a %(klass)s or when passed to %(klass)s.apply.
4586+
The function (or each function in a list/dict) must return an
4587+
object with the same length for the provided axis as the
4588+
calling %(klass)s.
45864589
4587-
Accepted Combinations are:
4590+
Accepted combinations are:
45884591
45894592
- string function name
45904593
- function
4591-
- list of functions
4592-
- dict of column names -> functions (or list of functions)
4594+
- list of functions and/or function names
4595+
- dict of axis labels -> functions, function names or list of such
4596+
%(axis)s
4597+
*args
4598+
Positional arguments to pass to `func`.
4599+
**kwargs
4600+
Keyword arguments to pass to `func`.
45934601
45944602
Returns
45954603
-------
45964604
transformed : %(klass)s
45974605
4598-
Examples
4599-
--------
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
4618-
--------
4619-
pandas.%(klass)s.aggregate
4620-
pandas.%(klass)s.apply
4606+
Raises
4607+
------
4608+
ValueError: if the returned %(klass)s has a different length than self.
46214609
""")
46224610

46234611
# ----------------------------------------------------------------------
@@ -9401,7 +9389,7 @@ def ewm(self, com=None, span=None, halflife=None, alpha=None,
94019389

94029390
cls.ewm = ewm
94039391

9404-
@Appender(_shared_docs['transform'] % _shared_doc_kwargs)
9392+
@Appender(_shared_docs['transform'] % dict(axis="", **_shared_doc_kwargs))
94059393
def transform(self, func, *args, **kwargs):
94069394
result = self.agg(func, *args, **kwargs)
94079395
if is_scalar(result) or len(result) != len(self):

pandas/core/series.py

+33
Original file line numberDiff line numberDiff line change
@@ -3098,6 +3098,39 @@ def aggregate(self, func, axis=0, *args, **kwargs):
30983098

30993099
agg = aggregate
31003100

3101+
_transform_doc = dedent("""
3102+
Examples
3103+
--------
3104+
>>> s = pd.Series(range(5))
3105+
>>> s.transform(lambda x: (x - x.mean()) / x.std())
3106+
0 -1.264911
3107+
1 -0.632456
3108+
2 0.000000
3109+
3 0.632456
3110+
4 1.264911
3111+
dtype: float64
3112+
3113+
>>> s.transform([np.sqrt, np.exp])
3114+
sqrt exp
3115+
0 0.000000 1.000000
3116+
1 1.000000 2.718282
3117+
2 1.414214 7.389056
3118+
3 1.732051 20.085537
3119+
4 2.000000 54.598150
3120+
3121+
See also
3122+
--------
3123+
pandas.Series.aggregate
3124+
pandas.Series.apply
3125+
""")
3126+
3127+
@Appender(_transform_doc)
3128+
@Appender(generic._shared_docs['transform'] % _shared_doc_kwargs)
3129+
def transform(self, func, axis=0, *args, **kwargs):
3130+
# Validate the axis parameter
3131+
self._get_axis_number(axis)
3132+
return super(Series, self).transform(func, *args, **kwargs)
3133+
31013134
def apply(self, func, convert_dtype=True, args=(), **kwds):
31023135
"""
31033136
Invoke function on values of Series. Can be ufunc (a NumPy function

0 commit comments

Comments
 (0)