Skip to content

Commit 83e24ca

Browse files
committed
DOC: template groupby.transform doc-string
1 parent 80f30b4 commit 83e24ca

File tree

1 file changed

+55
-54
lines changed

1 file changed

+55
-54
lines changed

pandas/core/groupby.py

+55-54
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,55 @@
7272
pandas.Panel.%(name)s
7373
"""
7474

75+
_transform_template = """
76+
Call function producing a like-indexed %(klass)s on each group and
77+
return a %(klass)s having the same indexes as the original object
78+
filled with the transformed values
79+
80+
Parameters
81+
----------
82+
f : function
83+
Function to apply to each group
84+
85+
Notes
86+
-----
87+
Each group is endowed the attribute 'name' in case you need to know
88+
which group you are working on.
89+
90+
The current implementation imposes three requirements on f:
91+
92+
* f must return a value that either has the same shape as the input
93+
subframe or can be broadcast to the shape of the input subframe.
94+
For example, f returns a scalar it will be broadcast to have the
95+
same shape as the input subframe.
96+
* if this is a DataFrame, f must support application column-by-column
97+
in the subframe. If f also supports application to the entire subframe,
98+
then a fast path is used starting from the second chunk.
99+
* f must not mutate groups. Mutation is not supported and may
100+
produce unexpected results.
101+
102+
Returns
103+
-------
104+
%(klass)s
105+
106+
See also
107+
--------
108+
aggregate, transform
109+
110+
Examples
111+
--------
112+
>>> df = pd.DataFrame(np.repeat(np.arange(10), 3).reshape(-1, 3),
113+
columns=list('ABC'))
114+
>>> grouped = df.groupby(df.index // 3)
115+
116+
# Same shape
117+
>>> grouped.%(selected)stransform(lambda x: (x - x.mean()) / x.std())
118+
119+
# Broadcastable
120+
>>> grouped.%(selected)stransform(lambda x: x.max() - x.min())
121+
122+
"""
123+
75124
# special case to prevent duplicate plots when catching exceptions when
76125
# forwarding methods from NDFrames
77126
_plotting_methods = frozenset(['plot', 'boxplot', 'hist'])
@@ -2860,25 +2909,9 @@ def _aggregate_named(self, func, *args, **kwargs):
28602909

28612910
return result
28622911

2912+
@Substitution(klass='Series', selected='A.')
2913+
@Appender(_transform_template)
28632914
def transform(self, func, *args, **kwargs):
2864-
"""
2865-
Call function producing a like-indexed Series on each group and return
2866-
a Series with the transformed values
2867-
2868-
Parameters
2869-
----------
2870-
func : function
2871-
To apply to each group. Should return a Series with the same index
2872-
2873-
Examples
2874-
--------
2875-
>>> grouped.transform(lambda x: (x - x.mean()) / x.std())
2876-
2877-
Returns
2878-
-------
2879-
transformed : Series
2880-
"""
2881-
28822915
func = self._is_cython_func(func) or func
28832916

28842917
# if string function
@@ -3633,42 +3666,9 @@ def _transform_general(self, func, *args, **kwargs):
36333666
axis=self.axis, verify_integrity=False)
36343667
return self._set_result_index_ordered(concatenated)
36353668

3669+
@Substitution(klass='DataFrame', selected='')
3670+
@Appender(_transform_template)
36363671
def transform(self, func, *args, **kwargs):
3637-
"""
3638-
Call function producing a like-indexed DataFrame on each group and
3639-
return a DataFrame having the same indexes as the original object
3640-
filled with the transformed values
3641-
3642-
Parameters
3643-
----------
3644-
f : function
3645-
Function to apply to each subframe
3646-
3647-
Notes
3648-
-----
3649-
Each subframe is endowed the attribute 'name' in case you need to know
3650-
which group you are working on.
3651-
3652-
The current implementation imposes three requirements on f:
3653-
3654-
* f must return a value that either has the same shape as the input
3655-
subframe or can be broadcast to the shape of the input subframe.
3656-
For example, f returns a scalar it will be broadcast to have the
3657-
same shape as the input subframe.
3658-
* f must support application column-by-column in the subframe. If f
3659-
also supports application to the entire subframe, then a fast path
3660-
is used starting from the second chunk.
3661-
* f must not mutate subframes. Mutation is not supported and may
3662-
produce unexpected results.
3663-
3664-
Examples
3665-
--------
3666-
>>> grouped = df.groupby(lambda x: mapping[x])
3667-
# Same shape
3668-
>>> grouped.transform(lambda x: (x - x.mean()) / x.std())
3669-
# Broadcastable
3670-
>>> grouped.transform(lambda x: x.max() - x.min())
3671-
"""
36723672

36733673
# optimized transforms
36743674
func = self._is_cython_func(func) or func
@@ -3784,7 +3784,8 @@ def filter(self, func, dropna=True, *args, **kwargs): # noqa
37843784
37853785
Examples
37863786
--------
3787-
>>> grouped = df.groupby(lambda x: mapping[x])
3787+
>>> df = pd.DataFrame(np.random.randn(10, 3), columns=list('ABC'))
3788+
>>> grouped = df.groupby(df.index % 3)
37883789
>>> grouped.filter(lambda x: x['A'].sum() + x['B'].sum() > 0)
37893790
"""
37903791

0 commit comments

Comments
 (0)