From 5d0cf00a9d9aad47d5624bd2512ba24199dc7887 Mon Sep 17 00:00:00 2001 From: Josquin Larsen <136840153+josquinlarsen@users.noreply.github.com> Date: Sun, 30 Nov 2025 17:33:26 +0000 Subject: [PATCH 1/8] fixing doc formatting issues for pipe --- pandas/core/groupby/groupby.py | 82 ++++++++++++++++++++++++++-------- 1 file changed, 63 insertions(+), 19 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 2c8ec599a19ef..11b1f31d4331a 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -64,7 +64,6 @@ class providing the base-class of operations. Pandas4Warning, ) from pandas.util._decorators import ( - Appender, Substitution, cache_readonly, doc, @@ -738,13 +737,66 @@ def pipe( **kwargs: Any, ) -> T: ... - @Substitution( - klass="GroupBy", - examples=dedent( - """\ - >>> df = pd.DataFrame({'A': 'a b a b'.split(), 'B': [1, 2, 3, 4]}) + def pipe( + self, + func: Callable[Concatenate[Self, P], T] | tuple[Callable[..., T], str], + *args: Any, + **kwargs: Any, + ) -> T: + """ + Apply a ``func`` with arguments to this GroupBy object and return its result. + + Use `.pipe` when you want to improve readability by chaining together + functions that expect Series, DataFrames, GroupBy or Resampler objects. + Instead of writing + + >>> h = lambda x, arg2, arg3: x + 1 - arg2 * arg3 + >>> g = lambda x, arg1: x * 5 / arg1 + >>> f = lambda x: x**4 + >>> df = pd.DataFrame([["a", 4], ["b", 5]], columns=["group", "value"]) + >>> h(g(f(df.groupby("group")), arg1=1), arg2=2, arg3=3) # doctest: +SKIP + + You can write + + >>> ( + ... df.groupby("group").pipe(f).pipe(g, arg1=1).pipe(h, arg2=2, arg3=3) + ... ) # doctest: +SKIP + + which is much more readable. + + Parameters + ---------- + func : callable or tuple of (callable, str) + Function to apply to this GroupBy object or, alternatively, + a `(callable, data_keyword)` tuple where `data_keyword` is a + string indicating the keyword of `callable` that expects the + GroupBy object. + args : iterable, optional + Positional arguments passed into `func`. + kwargs : dict, optional + A dictionary of keyword arguments passed into `func`. + + Returns + ------- + the return type of `func`. + + See Also + -------- + Series.pipe : Apply a function with arguments to a series. + DataFrame.pipe : Apply a function with arguments to a dataframe. + apply : Apply function to each group instead of to the + full GroupBy object. + + Notes + ----- + See more `here + `_ + + Examples + -------- + >>> df = pd.DataFrame({"A": "a b a b".split(), "B": [1, 2, 3, 4]}) >>> df - A B + A B 0 a 1 1 b 2 2 a 3 @@ -753,20 +805,12 @@ def pipe( To get the difference between each groups maximum and minimum value in one pass, you can do - >>> df.groupby('A').pipe(lambda x: x.max() - x.min()) - B + >>> df.groupby("A").pipe(lambda x: x.max() - x.min()) + B A a 2 - b 2""" - ), - ) - @Appender(_pipe_template) - def pipe( - self, - func: Callable[Concatenate[Self, P], T] | tuple[Callable[..., T], str], - *args: Any, - **kwargs: Any, - ) -> T: + b 2 + """ return com.pipe(self, func, *args, **kwargs) @final From 37387b37b3521d9f134e0afaed7cae31d52103df Mon Sep 17 00:00:00 2001 From: Josquin Larsen <136840153+josquinlarsen@users.noreply.github.com> Date: Sun, 30 Nov 2025 18:41:51 +0000 Subject: [PATCH 2/8] fix missing asterisks for args kwargs --- pandas/core/groupby/groupby.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 11b1f31d4331a..4d5920cfdcbd6 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -771,9 +771,9 @@ def pipe( a `(callable, data_keyword)` tuple where `data_keyword` is a string indicating the keyword of `callable` that expects the GroupBy object. - args : iterable, optional + *args : iterable, optional Positional arguments passed into `func`. - kwargs : dict, optional + **kwargs : dict, optional A dictionary of keyword arguments passed into `func`. Returns From 6e3c4b15998324f5ac416b178d7e7ba299da6bee Mon Sep 17 00:00:00 2001 From: Josquin Larsen <136840153+josquinlarsen@users.noreply.github.com> Date: Thu, 4 Dec 2025 19:10:06 +0000 Subject: [PATCH 3/8] add return type of GroupBy object to docstring --- pandas/core/groupby/groupby.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 4d5920cfdcbd6..072d6f42f00d1 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -778,7 +778,8 @@ def pipe( Returns ------- - the return type of `func`. + GroupBy + the return type of `func`. See Also -------- From ec45a65e1ce205b2619393456c6d7e161f0e53cf Mon Sep 17 00:00:00 2001 From: Josquin Larsen <136840153+josquinlarsen@users.noreply.github.com> Date: Thu, 4 Dec 2025 19:58:53 +0000 Subject: [PATCH 4/8] capitalize return type description --- pandas/core/groupby/groupby.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 072d6f42f00d1..b775f08d1d07e 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -779,7 +779,7 @@ def pipe( Returns ------- GroupBy - the return type of `func`. + The return type of `func`. See Also -------- From 747f35d77f0d4c63aaa7a3e973c63a0ea41d5693 Mon Sep 17 00:00:00 2001 From: Josquin Larsen <136840153+josquinlarsen@users.noreply.github.com> Date: Fri, 5 Dec 2025 00:08:55 +0000 Subject: [PATCH 5/8] fix examples formatting --- pandas/core/groupby/groupby.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index b775f08d1d07e..2f7bb5f87c343 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -797,7 +797,7 @@ def pipe( -------- >>> df = pd.DataFrame({"A": "a b a b".split(), "B": [1, 2, 3, 4]}) >>> df - A B + A B 0 a 1 1 b 2 2 a 3 @@ -807,8 +807,7 @@ def pipe( pass, you can do >>> df.groupby("A").pipe(lambda x: x.max() - x.min()) - B - A + A a 2 b 2 """ From 66a071bf7ed63b054e04133adada32fa3fb0ebb1 Mon Sep 17 00:00:00 2001 From: Josquin Larsen <136840153+josquinlarsen@users.noreply.github.com> Date: Fri, 5 Dec 2025 01:12:22 +0000 Subject: [PATCH 6/8] try removing A from second ex --- pandas/core/groupby/groupby.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 2f7bb5f87c343..52436d7261794 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -807,7 +807,6 @@ def pipe( pass, you can do >>> df.groupby("A").pipe(lambda x: x.max() - x.min()) - A a 2 b 2 """ From a08b2f4cfa3bbe9e000710ecab782f60637d3258 Mon Sep 17 00:00:00 2001 From: Josquin Larsen <136840153+josquinlarsen@users.noreply.github.com> Date: Fri, 5 Dec 2025 01:17:54 +0000 Subject: [PATCH 7/8] fixing example to match input cols A, B --- pandas/core/groupby/groupby.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 52436d7261794..4f9fd3e2c0cfb 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -807,6 +807,7 @@ def pipe( pass, you can do >>> df.groupby("A").pipe(lambda x: x.max() - x.min()) + A B a 2 b 2 """ From d8d1d1859024ad73cf76fe3a3318e3c873a497c3 Mon Sep 17 00:00:00 2001 From: Josquin Larsen <136840153+josquinlarsen@users.noreply.github.com> Date: Fri, 5 Dec 2025 01:51:33 +0000 Subject: [PATCH 8/8] try space between B and A --- pandas/core/groupby/groupby.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 4f9fd3e2c0cfb..326e4ffde815b 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -807,7 +807,8 @@ def pipe( pass, you can do >>> df.groupby("A").pipe(lambda x: x.max() - x.min()) - A B + B + A a 2 b 2 """