|
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,30 @@ def apply_func(values, begin, end, min_periods, raw=raw):
|
1529 | 1540 |
|
1530 | 1541 | return apply_func
|
1531 | 1542 |
|
| 1543 | + @overload |
| 1544 | + def pipe( |
| 1545 | + self, |
| 1546 | + func: Callable[Concatenate[Self, P], T], |
| 1547 | + *args: P.args, |
| 1548 | + **kwargs: P.kwargs, |
| 1549 | + ) -> T: ... |
| 1550 | + |
| 1551 | + @overload |
| 1552 | + def pipe( |
| 1553 | + self, |
| 1554 | + func: tuple[Callable[..., T], str], |
| 1555 | + *args: Any, |
| 1556 | + **kwargs: Any, |
| 1557 | + ) -> T: ... |
| 1558 | + |
| 1559 | + def pipe( |
| 1560 | + self, |
| 1561 | + func: Callable[Concatenate[Self, P], T] | tuple[Callable[..., T], str], |
| 1562 | + *args: Any, |
| 1563 | + **kwargs: Any, |
| 1564 | + ) -> T: |
| 1565 | + return com.pipe(self, func, *args, **kwargs) |
| 1566 | + |
1532 | 1567 | def sum(
|
1533 | 1568 | self,
|
1534 | 1569 | numeric_only: bool = False,
|
@@ -2044,6 +2079,54 @@ def apply(
|
2044 | 2079 | kwargs=kwargs,
|
2045 | 2080 | )
|
2046 | 2081 |
|
| 2082 | + @overload |
| 2083 | + def pipe( |
| 2084 | + self, |
| 2085 | + func: Callable[Concatenate[Self, P], T], |
| 2086 | + *args: P.args, |
| 2087 | + **kwargs: P.kwargs, |
| 2088 | + ) -> T: ... |
| 2089 | + |
| 2090 | + @overload |
| 2091 | + def pipe( |
| 2092 | + self, |
| 2093 | + func: tuple[Callable[..., T], str], |
| 2094 | + *args: Any, |
| 2095 | + **kwargs: Any, |
| 2096 | + ) -> T: ... |
| 2097 | + |
| 2098 | + @final |
| 2099 | + @Substitution( |
| 2100 | + klass="Rolling", |
| 2101 | + examples=""" |
| 2102 | + >>> df = pd.DataFrame({'A': [1, 2, 3, 4]}, |
| 2103 | + ... index=pd.date_range('2012-08-02', periods=4)) |
| 2104 | + >>> df |
| 2105 | + A |
| 2106 | + 2012-08-02 1 |
| 2107 | + 2012-08-03 2 |
| 2108 | + 2012-08-04 3 |
| 2109 | + 2012-08-05 4 |
| 2110 | +
|
| 2111 | + To get the difference between each rolling 2-day window's maximum and minimum |
| 2112 | + value in one pass, you can do |
| 2113 | +
|
| 2114 | + >>> df.rolling('2D').pipe(lambda x: x.max() - x.min()) |
| 2115 | + A |
| 2116 | + 2012-08-02 0.0 |
| 2117 | + 2012-08-03 1.0 |
| 2118 | + 2012-08-04 1.0 |
| 2119 | + 2012-08-05 1.0""", |
| 2120 | + ) |
| 2121 | + @Appender(template_pipe) |
| 2122 | + def pipe( |
| 2123 | + self, |
| 2124 | + func: Callable[Concatenate[Self, P], T] | tuple[Callable[..., T], str], |
| 2125 | + *args: Any, |
| 2126 | + **kwargs: Any, |
| 2127 | + ) -> T: |
| 2128 | + return super().pipe(func, *args, **kwargs) |
| 2129 | + |
2047 | 2130 | @doc(
|
2048 | 2131 | template_header,
|
2049 | 2132 | create_section_header("Parameters"),
|
|
0 commit comments