|
72 | 72 | pandas.Panel.%(name)s |
73 | 73 | """ |
74 | 74 |
|
| 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 | + |
75 | 124 | # special case to prevent duplicate plots when catching exceptions when |
76 | 125 | # forwarding methods from NDFrames |
77 | 126 | _plotting_methods = frozenset(['plot', 'boxplot', 'hist']) |
@@ -2860,25 +2909,9 @@ def _aggregate_named(self, func, *args, **kwargs): |
2860 | 2909 |
|
2861 | 2910 | return result |
2862 | 2911 |
|
| 2912 | + @Substitution(klass='Series', selected='A.') |
| 2913 | + @Appender(_transform_template) |
2863 | 2914 | 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 | | - |
2882 | 2915 | func = self._is_cython_func(func) or func |
2883 | 2916 |
|
2884 | 2917 | # if string function |
@@ -3633,42 +3666,9 @@ def _transform_general(self, func, *args, **kwargs): |
3633 | 3666 | axis=self.axis, verify_integrity=False) |
3634 | 3667 | return self._set_result_index_ordered(concatenated) |
3635 | 3668 |
|
| 3669 | + @Substitution(klass='DataFrame', selected='') |
| 3670 | + @Appender(_transform_template) |
3636 | 3671 | 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 | | - """ |
3672 | 3672 |
|
3673 | 3673 | # optimized transforms |
3674 | 3674 | func = self._is_cython_func(func) or func |
@@ -3784,7 +3784,8 @@ def filter(self, func, dropna=True, *args, **kwargs): # noqa |
3784 | 3784 |
|
3785 | 3785 | Examples |
3786 | 3786 | -------- |
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) |
3788 | 3789 | >>> grouped.filter(lambda x: x['A'].sum() + x['B'].sum() > 0) |
3789 | 3790 | """ |
3790 | 3791 |
|
|
0 commit comments