Skip to content

Commit b349ba4

Browse files
committed
Replace @substitution and @appender with inline docstring for pipe function
1 parent 95624ca commit b349ba4

File tree

1 file changed

+63
-17
lines changed

1 file changed

+63
-17
lines changed

pandas/core/groupby/groupby.py

Lines changed: 63 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ class providing the base-class of operations.
6464
Pandas4Warning,
6565
)
6666
from pandas.util._decorators import (
67-
Appender,
6867
Substitution,
6968
cache_readonly,
7069
doc,
@@ -738,13 +737,68 @@ def pipe(
738737
**kwargs: Any,
739738
) -> T: ...
740739

741-
@Substitution(
742-
klass="GroupBy",
743-
examples=dedent(
744-
"""\
740+
def pipe(
741+
self,
742+
func: Callable[Concatenate[Self, P], T] | tuple[Callable[..., T], str],
743+
*args: Any,
744+
**kwargs: Any,
745+
) -> T:
746+
"""
747+
Apply a ``func`` with arguments to this GroupBy object and return its result.
748+
749+
Use `.pipe` when you want to improve readability by chaining together
750+
functions that expect Series, DataFrames, GroupBy or Resampler objects.
751+
Instead of writing
752+
753+
>>> h = lambda x, arg2, arg3: x + 1 - arg2 * arg3
754+
>>> g = lambda x, arg1: x * 5 / arg1
755+
>>> f = lambda x: x ** 4
756+
>>> df = pd.DataFrame([["a", 4], ["b", 5]], columns=["group", "value"])
757+
>>> h(g(f(df.groupby('group')), arg1=1), arg2=2, arg3=3) # doctest: +SKIP
758+
759+
You can write
760+
761+
>>> (df.groupby('group')
762+
... .pipe(f)
763+
... .pipe(g, arg1=1)
764+
... .pipe(h, arg2=2, arg3=3)) # doctest: +SKIP
765+
766+
which is much more readable.
767+
768+
Parameters
769+
----------
770+
func : callable or tuple of (callable, str)
771+
Function to apply to this GroupBy object or, alternatively,
772+
a `(callable, data_keyword)` tuple where `data_keyword` is a
773+
string indicating the keyword of `callable` that expects the
774+
GroupBy object.
775+
*args : iterable, optional
776+
Positional arguments passed into `func`.
777+
**kwargs : dict, optional
778+
A dictionary of keyword arguments passed into `func`.
779+
780+
Returns
781+
-------
782+
GroupBy
783+
The original object with the function `func` applied.
784+
785+
See Also
786+
--------
787+
Series.pipe : Apply a function with arguments to a series.
788+
DataFrame.pipe: Apply a function with arguments to a dataframe.
789+
apply : Apply function to each group instead of to the
790+
full GroupBy object.
791+
792+
Notes
793+
-----
794+
See more `here
795+
<https://pandas.pydata.org/pandas-docs/stable/user_guide/groupby.html#piping-function-calls>`_
796+
797+
Examples
798+
--------
745799
>>> df = pd.DataFrame({'A': 'a b a b'.split(), 'B': [1, 2, 3, 4]})
746800
>>> df
747-
A B
801+
A B
748802
0 a 1
749803
1 b 2
750804
2 a 3
@@ -754,19 +808,11 @@ def pipe(
754808
pass, you can do
755809
756810
>>> df.groupby('A').pipe(lambda x: x.max() - x.min())
757-
B
811+
B
758812
A
759813
a 2
760-
b 2"""
761-
),
762-
)
763-
@Appender(_pipe_template)
764-
def pipe(
765-
self,
766-
func: Callable[Concatenate[Self, P], T] | tuple[Callable[..., T], str],
767-
*args: Any,
768-
**kwargs: Any,
769-
) -> T:
814+
b 2
815+
"""
770816
return com.pipe(self, func, *args, **kwargs)
771817

772818
@final

0 commit comments

Comments
 (0)