-
-
Notifications
You must be signed in to change notification settings - Fork 19.4k
DOC: 62437 Replace @Substitution and @Appender in core/groupby/groupby.py #63234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
5d0cf00
37387b3
6e3c4b1
ec45a65
747f35d
66a071b
a08b2f4
d8d1d18
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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,67 @@ 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 | ||
| ------- | ||
| GroupBy | ||
| 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 | ||
| <https://pandas.pydata.org/pandas-docs/stable/user_guide/groupby.html#piping-function-calls>`_ | ||
|
|
||
| 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 +806,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 | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change is wrong, the columns should be adjusted. Is pre-commit doing this with the ruff formater?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed! I think this is a holdover from the contents of the generated docstring I printed to console.