|
41 | 41 |
|
42 | 42 |
|
43 | 43 | class BaseIndexer:
|
44 |
| - """Base class for window bounds calculations.""" |
| 44 | + """ |
| 45 | + Base class for window bounds calculations. |
| 46 | +
|
| 47 | + Examples |
| 48 | + -------- |
| 49 | + >>> from pandas.api.indexers import BaseIndexer |
| 50 | + >>> class CustomIndexer(BaseIndexer): |
| 51 | + ... def get_window_bounds(self, num_values, min_periods, center, closed, step): |
| 52 | + ... start = np.empty(num_values, dtype=np.int64) |
| 53 | + ... end = np.empty(num_values, dtype=np.int64) |
| 54 | + ... for i in range(num_values): |
| 55 | + ... start[i] = i |
| 56 | + ... end[i] = i + self.window_size |
| 57 | + ... return start, end |
| 58 | + >>> df = pd.DataFrame({"values": range(5)}) |
| 59 | + >>> indexer = CustomIndexer(window_size=2) |
| 60 | + >>> df.rolling(indexer).sum() |
| 61 | + values |
| 62 | + 0 1.0 |
| 63 | + 1 3.0 |
| 64 | + 2 5.0 |
| 65 | + 3 7.0 |
| 66 | + 4 4.0 |
| 67 | + """ |
45 | 68 |
|
46 | 69 | def __init__(
|
47 | 70 | self, index_array: np.ndarray | None = None, window_size: int = 0, **kwargs
|
48 | 71 | ) -> None:
|
49 |
| - """ |
50 |
| - Parameters |
51 |
| - ---------- |
52 |
| - **kwargs : |
53 |
| - keyword arguments that will be available when get_window_bounds is called |
54 |
| - """ |
55 | 72 | self.index_array = index_array
|
56 | 73 | self.window_size = window_size
|
57 | 74 | # Set user defined kwargs as attributes that can be used in get_window_bounds
|
@@ -127,7 +144,40 @@ def get_window_bounds(
|
127 | 144 |
|
128 | 145 |
|
129 | 146 | class VariableOffsetWindowIndexer(BaseIndexer):
|
130 |
| - """Calculate window boundaries based on a non-fixed offset such as a BusinessDay.""" |
| 147 | + """ |
| 148 | + Calculate window boundaries based on a non-fixed offset such as a BusinessDay. |
| 149 | +
|
| 150 | + Examples |
| 151 | + -------- |
| 152 | + >>> from pandas.api.indexers import VariableOffsetWindowIndexer |
| 153 | + >>> df = pd.DataFrame(range(10), index=pd.date_range("2020", periods=10)) |
| 154 | + >>> offset = pd.offsets.BDay(1) |
| 155 | + >>> indexer = VariableOffsetWindowIndexer(index=df.index, offset=offset) |
| 156 | + >>> df |
| 157 | + 0 |
| 158 | + 2020-01-01 0 |
| 159 | + 2020-01-02 1 |
| 160 | + 2020-01-03 2 |
| 161 | + 2020-01-04 3 |
| 162 | + 2020-01-05 4 |
| 163 | + 2020-01-06 5 |
| 164 | + 2020-01-07 6 |
| 165 | + 2020-01-08 7 |
| 166 | + 2020-01-09 8 |
| 167 | + 2020-01-10 9 |
| 168 | + >>> df.rolling(indexer).sum() |
| 169 | + 0 |
| 170 | + 2020-01-01 0.0 |
| 171 | + 2020-01-02 1.0 |
| 172 | + 2020-01-03 2.0 |
| 173 | + 2020-01-04 3.0 |
| 174 | + 2020-01-05 7.0 |
| 175 | + 2020-01-06 12.0 |
| 176 | + 2020-01-07 6.0 |
| 177 | + 2020-01-08 7.0 |
| 178 | + 2020-01-09 8.0 |
| 179 | + 2020-01-10 9.0 |
| 180 | + """ |
131 | 181 |
|
132 | 182 | def __init__(
|
133 | 183 | self,
|
|
0 commit comments