|
14 | 14 | TYPE_CHECKING, |
15 | 15 | Any, |
16 | 16 | Literal, |
| 17 | + final, |
| 18 | + overload, |
17 | 19 | ) |
18 | 20 |
|
19 | 21 | import numpy as np |
|
26 | 28 | import pandas._libs.window.aggregations as window_aggregations |
27 | 29 | from pandas.compat._optional import import_optional_dependency |
28 | 30 | from pandas.errors import DataError |
29 | | -from pandas.util._decorators import doc |
| 31 | +from pandas.util._decorators import ( |
| 32 | + Appender, |
| 33 | + Substitution, |
| 34 | + doc, |
| 35 | +) |
30 | 36 |
|
31 | 37 | from pandas.core.dtypes.common import ( |
32 | 38 | ensure_float64, |
|
81 | 87 | kwargs_scipy, |
82 | 88 | numba_notes, |
83 | 89 | template_header, |
| 90 | + template_pipe, |
84 | 91 | template_returns, |
85 | 92 | template_see_also, |
86 | 93 | window_agg_numba_parameters, |
|
102 | 109 |
|
103 | 110 | from pandas._typing import ( |
104 | 111 | ArrayLike, |
| 112 | + Concatenate, |
105 | 113 | NDFrameT, |
106 | 114 | QuantileInterpolation, |
| 115 | + P, |
| 116 | + Self, |
| 117 | + T, |
107 | 118 | WindowingRankType, |
108 | 119 | npt, |
109 | 120 | ) |
@@ -1529,6 +1540,14 @@ def apply_func(values, begin, end, min_periods, raw=raw): |
1529 | 1540 |
|
1530 | 1541 | return apply_func |
1531 | 1542 |
|
| 1543 | + def pipe( |
| 1544 | + self, |
| 1545 | + func: Callable[Concatenate[Self, P], T] | tuple[Callable[..., T], str], |
| 1546 | + *args: Any, |
| 1547 | + **kwargs: Any, |
| 1548 | + ) -> T: |
| 1549 | + return com.pipe(self, func, *args, **kwargs) |
| 1550 | + |
1532 | 1551 | def sum( |
1533 | 1552 | self, |
1534 | 1553 | numeric_only: bool = False, |
@@ -2044,6 +2063,54 @@ def apply( |
2044 | 2063 | kwargs=kwargs, |
2045 | 2064 | ) |
2046 | 2065 |
|
| 2066 | + @overload |
| 2067 | + def pipe( |
| 2068 | + self, |
| 2069 | + func: Callable[Concatenate[Self, P], T], |
| 2070 | + *args: P.args, |
| 2071 | + **kwargs: P.kwargs, |
| 2072 | + ) -> T: ... |
| 2073 | + |
| 2074 | + @overload |
| 2075 | + def pipe( |
| 2076 | + self, |
| 2077 | + func: tuple[Callable[..., T], str], |
| 2078 | + *args: Any, |
| 2079 | + **kwargs: Any, |
| 2080 | + ) -> T: ... |
| 2081 | + |
| 2082 | + @final |
| 2083 | + @Substitution( |
| 2084 | + klass="Rolling", |
| 2085 | + examples=""" |
| 2086 | + >>> df = pd.DataFrame({'A': [1, 2, 3, 4]}, |
| 2087 | + ... index=pd.date_range('2012-08-02', periods=4)) |
| 2088 | + >>> df |
| 2089 | + A |
| 2090 | + 2012-08-02 1 |
| 2091 | + 2012-08-03 2 |
| 2092 | + 2012-08-04 3 |
| 2093 | + 2012-08-05 4 |
| 2094 | +
|
| 2095 | + To get the difference between each rolling 2-day window's maximum and minimum |
| 2096 | + value in one pass, you can do |
| 2097 | +
|
| 2098 | + >>> df.rolling('2D').pipe(lambda x: x.max() - x.min()) |
| 2099 | + A |
| 2100 | + 2012-08-02 0 |
| 2101 | + 2012-08-03 1 |
| 2102 | + 2012-08-04 1 |
| 2103 | + 2012-08-05 1""", |
| 2104 | + ) |
| 2105 | + @Appender(template_pipe) |
| 2106 | + def pipe( |
| 2107 | + self, |
| 2108 | + func: Callable[Concatenate[Self, P], T] | tuple[Callable[..., T], str], |
| 2109 | + *args: Any, |
| 2110 | + **kwargs: Any, |
| 2111 | + ) -> T: |
| 2112 | + return super().pipe(func, *args, **kwargs) |
| 2113 | + |
2047 | 2114 | @doc( |
2048 | 2115 | template_header, |
2049 | 2116 | create_section_header("Parameters"), |
|
0 commit comments