|
191 | 191 | dtype: int64
|
192 | 192 | """)
|
193 | 193 |
|
| 194 | +_pipe_template = """\ |
| 195 | +Apply a function ``func`` with arguments to this %(klass)s object and return |
| 196 | +the function's result. |
| 197 | +
|
| 198 | +%(versionadded)s |
| 199 | +
|
| 200 | +Use ``.pipe`` when you want to improve readability by chaining together |
| 201 | +functions that expect Series, DataFrames, GroupBy or Resampler objects. |
| 202 | +Instead of writing |
| 203 | +
|
| 204 | +>>> h(g(f(df.groupby('group')), arg1=a), arg2=b, arg3=c) |
| 205 | +
|
| 206 | +You can write |
| 207 | +
|
| 208 | +>>> (df.groupby('group') |
| 209 | +... .pipe(f) |
| 210 | +... .pipe(g, arg1=a) |
| 211 | +... .pipe(h, arg2=b, arg3=c)) |
| 212 | +
|
| 213 | +which is much more readable. |
| 214 | +
|
| 215 | +Parameters |
| 216 | +---------- |
| 217 | +func : callable or tuple of (callable, string) |
| 218 | + Function to apply to this %(klass)s object or, alternatively, |
| 219 | + a ``(callable, data_keyword)`` tuple where ``data_keyword`` is a |
| 220 | + string indicating the keyword of ``callable`` that expects the |
| 221 | + %(klass)s object. |
| 222 | +args : iterable, optional |
| 223 | + positional arguments passed into ``func``. |
| 224 | +kwargs : dict, optional |
| 225 | + a dictionary of keyword arguments passed into ``func``. |
| 226 | +
|
| 227 | +Returns |
| 228 | +------- |
| 229 | +object : the return type of ``func``. |
| 230 | +
|
| 231 | +Notes |
| 232 | +----- |
| 233 | +See more `here |
| 234 | +<http://pandas.pydata.org/pandas-docs/stable/groupby.html#pipe>`_ |
| 235 | +
|
| 236 | +Examples |
| 237 | +-------- |
| 238 | +%(examples)s |
| 239 | +
|
| 240 | +See Also |
| 241 | +-------- |
| 242 | +pandas.Series.pipe : Apply a function with arguments to a series |
| 243 | +pandas.DataFrame.pipe: Apply a function with arguments to a dataframe |
| 244 | +apply : Apply function to each group instead of to the |
| 245 | + full %(klass)s object. |
| 246 | +""" |
| 247 | + |
194 | 248 | _transform_template = """
|
195 | 249 | Call function producing a like-indexed %(klass)s on each group and
|
196 | 250 | return a %(klass)s having the same indexes as the original object
|
@@ -676,6 +730,29 @@ def __getattr__(self, attr):
|
676 | 730 | raise AttributeError("%r object has no attribute %r" %
|
677 | 731 | (type(self).__name__, attr))
|
678 | 732 |
|
| 733 | + @Substitution(klass='GroupBy', |
| 734 | + versionadded='.. versionadded:: 0.21.0', |
| 735 | + examples="""\ |
| 736 | +>>> df = pd.DataFrame({'A': 'a b a b'.split(), 'B': [1, 2, 3, 4]}) |
| 737 | +>>> df |
| 738 | + A B |
| 739 | +0 a 1 |
| 740 | +1 b 2 |
| 741 | +2 a 3 |
| 742 | +3 b 4 |
| 743 | +
|
| 744 | +To get the difference between each groups maximum and minimum value in one |
| 745 | +pass, you can do |
| 746 | +
|
| 747 | +>>> df.groupby('A').pipe(lambda x: x.max() - x.min()) |
| 748 | + B |
| 749 | +A |
| 750 | +a 2 |
| 751 | +b 2""") |
| 752 | + @Appender(_pipe_template) |
| 753 | + def pipe(self, func, *args, **kwargs): |
| 754 | + return _pipe(self, func, *args, **kwargs) |
| 755 | + |
679 | 756 | plot = property(GroupByPlot)
|
680 | 757 |
|
681 | 758 | def _make_wrapper(self, name):
|
@@ -1779,54 +1856,6 @@ def tail(self, n=5):
|
1779 | 1856 | mask = self._cumcount_array(ascending=False) < n
|
1780 | 1857 | return self._selected_obj[mask]
|
1781 | 1858 |
|
1782 |
| - def pipe(self, func, *args, **kwargs): |
1783 |
| - """ Apply a function with arguments to this GroupBy object, |
1784 |
| -
|
1785 |
| - .. versionadded:: 0.21.0 |
1786 |
| -
|
1787 |
| - Parameters |
1788 |
| - ---------- |
1789 |
| - func : callable or tuple of (callable, string) |
1790 |
| - Function to apply to this GroupBy object or, alternatively, a |
1791 |
| - ``(callable, data_keyword)`` tuple where ``data_keyword`` is a |
1792 |
| - string indicating the keyword of ``callable`` that expects the |
1793 |
| - GroupBy object. |
1794 |
| - args : iterable, optional |
1795 |
| - positional arguments passed into ``func``. |
1796 |
| - kwargs : dict, optional |
1797 |
| - a dictionary of keyword arguments passed into ``func``. |
1798 |
| -
|
1799 |
| - Returns |
1800 |
| - ------- |
1801 |
| - object : the return type of ``func``. |
1802 |
| -
|
1803 |
| - Notes |
1804 |
| - ----- |
1805 |
| - Use ``.pipe`` when chaining together functions that expect |
1806 |
| - Series, DataFrames or GroupBy objects. Instead of writing |
1807 |
| -
|
1808 |
| - >>> f(g(h(df.groupby('group')), arg1=a), arg2=b, arg3=c) |
1809 |
| -
|
1810 |
| - You can write |
1811 |
| -
|
1812 |
| - >>> (df |
1813 |
| - ... .groupby('group') |
1814 |
| - ... .pipe(f, arg1) |
1815 |
| - ... .pipe(g, arg2) |
1816 |
| - ... .pipe(h, arg3)) |
1817 |
| -
|
1818 |
| - See more `here |
1819 |
| - <http://pandas.pydata.org/pandas-docs/stable/groupby.html#pipe>`_ |
1820 |
| -
|
1821 |
| - See Also |
1822 |
| - -------- |
1823 |
| - pandas.Series.pipe : Apply a function with arguments to a series |
1824 |
| - pandas.DataFrame.pipe: Apply a function with arguments to a dataframe |
1825 |
| - apply : Apply function to each group instead of to the |
1826 |
| - full GroupBy object. |
1827 |
| - """ |
1828 |
| - return _pipe(self, func, *args, **kwargs) |
1829 |
| - |
1830 | 1859 |
|
1831 | 1860 | GroupBy._add_numeric_operations()
|
1832 | 1861 |
|
|
0 commit comments