Skip to content

Commit 5d0cf00

Browse files
committed
fixing doc formatting issues for pipe
1 parent fb517ba commit 5d0cf00

File tree

1 file changed

+63
-19
lines changed

1 file changed

+63
-19
lines changed

pandas/core/groupby/groupby.py

Lines changed: 63 additions & 19 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,66 @@ 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+
the return type of `func`.
782+
783+
See Also
784+
--------
785+
Series.pipe : Apply a function with arguments to a series.
786+
DataFrame.pipe : Apply a function with arguments to a dataframe.
787+
apply : Apply function to each group instead of to the
788+
full GroupBy object.
789+
790+
Notes
791+
-----
792+
See more `here
793+
<https://pandas.pydata.org/pandas-docs/stable/user_guide/groupby.html#piping-function-calls>`_
794+
795+
Examples
796+
--------
797+
>>> df = pd.DataFrame({"A": "a b a b".split(), "B": [1, 2, 3, 4]})
746798
>>> df
747-
A B
799+
A B
748800
0 a 1
749801
1 b 2
750802
2 a 3
@@ -753,20 +805,12 @@ def pipe(
753805
To get the difference between each groups maximum and minimum value in one
754806
pass, you can do
755807
756-
>>> df.groupby('A').pipe(lambda x: x.max() - x.min())
757-
B
808+
>>> df.groupby("A").pipe(lambda x: x.max() - x.min())
809+
B
758810
A
759811
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:
812+
b 2
813+
"""
770814
return com.pipe(self, func, *args, **kwargs)
771815

772816
@final

0 commit comments

Comments
 (0)