Skip to content

Commit fbece39

Browse files
committed
Replace @appender and @substitution decorator with inline docstrings in core/window/expanding.py
1 parent f48deef commit fbece39

File tree

1 file changed

+78
-31
lines changed

1 file changed

+78
-31
lines changed

pandas/core/window/expanding.py

Lines changed: 78 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,11 @@
1010
overload,
1111
)
1212

13-
from pandas.util._decorators import (
14-
Appender,
15-
Substitution,
16-
)
17-
1813
from pandas.core.indexers.objects import (
1914
BaseIndexer,
2015
ExpandingIndexer,
2116
GroupbyIndexer,
2217
)
23-
from pandas.core.window.doc import (
24-
template_pipe,
25-
)
2618
from pandas.core.window.rolling import (
2719
BaseWindowGroupby,
2820
RollingAndExpandingMixin,
@@ -343,35 +335,90 @@ def pipe(
343335
) -> T: ...
344336

345337
@final
346-
@Substitution(
347-
klass="Expanding",
348-
examples="""
349-
>>> df = pd.DataFrame({'A': [1, 2, 3, 4]},
350-
... index=pd.date_range('2012-08-02', periods=4))
351-
>>> df
352-
A
353-
2012-08-02 1
354-
2012-08-03 2
355-
2012-08-04 3
356-
2012-08-05 4
357-
358-
To get the difference between each expanding window's maximum and minimum
359-
value in one pass, you can do
360-
361-
>>> df.expanding().pipe(lambda x: x.max() - x.min())
362-
A
363-
2012-08-02 0.0
364-
2012-08-03 1.0
365-
2012-08-04 2.0
366-
2012-08-05 3.0""",
367-
)
368-
@Appender(template_pipe)
369338
def pipe(
370339
self,
371340
func: Callable[Concatenate[Self, P], T] | tuple[Callable[..., T], str],
372341
*args: Any,
373342
**kwargs: Any,
374343
) -> T:
344+
"""
345+
Apply a ``func`` with arguments to this Expanding object and return its result.
346+
347+
Use `.pipe` when you want to improve readability by chaining together
348+
functions that expect Series, DataFrames, GroupBy, Rolling, Expanding or
349+
Resampler
350+
objects.
351+
Instead of writing
352+
353+
>>> h = lambda x, arg2, arg3: x + 1 - arg2 * arg3
354+
>>> g = lambda x, arg1: x * 5 / arg1
355+
>>> f = lambda x: x**4
356+
>>> df = pd.DataFrame(
357+
... {"A": [1, 2, 3, 4]},
358+
index=pd.date_range("2012-08-02", periods=4)
359+
... )
360+
>>> h(g(f(df.rolling("2D")), arg1=1), arg2=2, arg3=3) # doctest: +SKIP
361+
362+
You can write
363+
364+
>>> (
365+
... df.rolling("2D").pipe(f).pipe(g, arg1=1).pipe(h, arg2=2, arg3=3)
366+
... ) # doctest: +SKIP
367+
368+
which is much more readable.
369+
370+
Parameters
371+
----------
372+
func : callable or tuple of (callable, str)
373+
Function to apply to this Expanding object or, alternatively,
374+
a `(callable, data_keyword)` tuple where `data_keyword` is a
375+
string indicating the keyword of `callable` that expects the
376+
Expanding object.
377+
*args : iterable, optional
378+
Positional arguments passed into `func`.
379+
**kwargs : dict, optional
380+
A dictionary of keyword arguments passed into `func`.
381+
382+
Returns
383+
-------
384+
Expanding
385+
The original object with the function `func` applied.
386+
387+
See Also
388+
--------
389+
Series.pipe : Apply a function with arguments to a series.
390+
DataFrame.pipe: Apply a function with arguments to a dataframe.
391+
apply : Apply function to each group instead of to the
392+
full Expanding object.
393+
394+
Notes
395+
-----
396+
See more `here
397+
<https://pandas.pydata.org/pandas-docs/stable/user_guide/groupby.html#piping-function-calls>`_
398+
399+
Examples
400+
--------
401+
402+
>>> df = pd.DataFrame(
403+
... {"A": [1, 2, 3, 4]}, index=pd.date_range("2012-08-02", periods=4)
404+
... )
405+
>>> df
406+
A
407+
2012-08-02 1
408+
2012-08-03 2
409+
2012-08-04 3
410+
2012-08-05 4
411+
412+
To get the difference between each expanding window's maximum and minimum
413+
value in one pass, you can do
414+
415+
>>> df.expanding().pipe(lambda x: x.max() - x.min())
416+
A
417+
2012-08-02 0.0
418+
2012-08-03 1.0
419+
2012-08-04 2.0
420+
2012-08-05 3.0
421+
"""
375422
return super().pipe(func, *args, **kwargs)
376423

377424
def sum(

0 commit comments

Comments
 (0)