Skip to content

Commit 09dba33

Browse files
DOC: 62437 Replace @substitution and @appender in core/groupby/groupby.py (#63234)
1 parent fa1360d commit 09dba33

File tree

1 file changed

+62
-17
lines changed

1 file changed

+62
-17
lines changed

pandas/core/groupby/groupby.py

Lines changed: 62 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,11 +737,65 @@ def pipe(
738737
**kwargs: Any,
739738
) -> T: ...
740739

741-
@Substitution(
742-
klass="GroupBy",
743-
examples=dedent(
744-
"""\
745-
>>> df = pd.DataFrame({'A': 'a b a b'.split(), 'B': [1, 2, 3, 4]})
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+
>>> (
762+
... df.groupby("group").pipe(f).pipe(g, arg1=1).pipe(h, arg2=2, arg3=3)
763+
... ) # doctest: +SKIP
764+
765+
which is much more readable.
766+
767+
Parameters
768+
----------
769+
func : callable or tuple of (callable, str)
770+
Function to apply to this GroupBy object or, alternatively,
771+
a `(callable, data_keyword)` tuple where `data_keyword` is a
772+
string indicating the keyword of `callable` that expects the
773+
GroupBy object.
774+
*args : iterable, optional
775+
Positional arguments passed into `func`.
776+
**kwargs : dict, optional
777+
A dictionary of keyword arguments passed into `func`.
778+
779+
Returns
780+
-------
781+
GroupBy
782+
The return type of `func`.
783+
784+
See Also
785+
--------
786+
Series.pipe : Apply a function with arguments to a series.
787+
DataFrame.pipe : Apply a function with arguments to a dataframe.
788+
apply : Apply function to each group instead of to the
789+
full GroupBy object.
790+
791+
Notes
792+
-----
793+
See more `here
794+
<https://pandas.pydata.org/pandas-docs/stable/user_guide/groupby.html#piping-function-calls>`_
795+
796+
Examples
797+
--------
798+
>>> df = pd.DataFrame({"A": "a b a b".split(), "B": [1, 2, 3, 4]})
746799
>>> df
747800
A B
748801
0 a 1
@@ -753,20 +806,12 @@ def pipe(
753806
To get the difference between each groups maximum and minimum value in one
754807
pass, you can do
755808
756-
>>> df.groupby('A').pipe(lambda x: x.max() - x.min())
809+
>>> df.groupby("A").pipe(lambda x: x.max() - x.min())
757810
B
758811
A
759812
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:
813+
b 2
814+
"""
770815
return com.pipe(self, func, *args, **kwargs)
771816

772817
@final

0 commit comments

Comments
 (0)