|
1 | 1 | """Indexer objects for computing start/end window bounds for rolling operations"""
|
| 2 | +from datetime import timedelta |
2 | 3 | from typing import Dict, Optional, Tuple, Type, Union
|
3 | 4 |
|
4 | 5 | import numpy as np
|
5 | 6 |
|
6 | 7 | from pandas._libs.window.indexers import calculate_variable_window_bounds
|
7 | 8 | from pandas.util._decorators import Appender
|
8 | 9 |
|
| 10 | +from pandas.tseries.offsets import Nano |
| 11 | + |
9 | 12 | get_window_bounds_doc = """
|
10 | 13 | Computes the bounds of a window.
|
11 | 14 |
|
@@ -104,6 +107,88 @@ def get_window_bounds(
|
104 | 107 | )
|
105 | 108 |
|
106 | 109 |
|
| 110 | +class NonFixedVariableWindowIndexer(BaseIndexer): |
| 111 | + """Calculate window boundaries based on a non-fixed offset such as a BusinessDay""" |
| 112 | + |
| 113 | + def __init__( |
| 114 | + self, |
| 115 | + index_array: Optional[np.ndarray] = None, |
| 116 | + window_size: int = 0, |
| 117 | + index=None, |
| 118 | + offset=None, |
| 119 | + **kwargs, |
| 120 | + ): |
| 121 | + super().__init__(index_array, window_size, **kwargs) |
| 122 | + self.index = index |
| 123 | + self.offset = offset |
| 124 | + |
| 125 | + @Appender(get_window_bounds_doc) |
| 126 | + def get_window_bounds( |
| 127 | + self, |
| 128 | + num_values: int = 0, |
| 129 | + min_periods: Optional[int] = None, |
| 130 | + center: Optional[bool] = None, |
| 131 | + closed: Optional[str] = None, |
| 132 | + ) -> Tuple[np.ndarray, np.ndarray]: |
| 133 | + |
| 134 | + # if windows is variable, default is 'right', otherwise default is 'both' |
| 135 | + if closed is None: |
| 136 | + closed = "right" if self.index is not None else "both" |
| 137 | + |
| 138 | + right_closed = closed in ["right", "both"] |
| 139 | + left_closed = closed in ["left", "both"] |
| 140 | + |
| 141 | + if self.index[num_values - 1] < self.index[0]: |
| 142 | + index_growth_sign = -1 |
| 143 | + else: |
| 144 | + index_growth_sign = 1 |
| 145 | + |
| 146 | + start = np.empty(num_values, dtype="int64") |
| 147 | + start.fill(-1) |
| 148 | + end = np.empty(num_values, dtype="int64") |
| 149 | + end.fill(-1) |
| 150 | + |
| 151 | + start[0] = 0 |
| 152 | + |
| 153 | + # right endpoint is closed |
| 154 | + if right_closed: |
| 155 | + end[0] = 1 |
| 156 | + # right endpoint is open |
| 157 | + else: |
| 158 | + end[0] = 0 |
| 159 | + |
| 160 | + # start is start of slice interval (including) |
| 161 | + # end is end of slice interval (not including) |
| 162 | + for i in range(1, num_values): |
| 163 | + end_bound = self.index[i] |
| 164 | + start_bound = self.index[i] - index_growth_sign * self.offset |
| 165 | + |
| 166 | + # left endpoint is closed |
| 167 | + if left_closed: |
| 168 | + start_bound -= Nano(1) |
| 169 | + |
| 170 | + # advance the start bound until we are |
| 171 | + # within the constraint |
| 172 | + start[i] = i |
| 173 | + for j in range(start[i - 1], i): |
| 174 | + if (self.index[j] - start_bound) * index_growth_sign > timedelta(0): |
| 175 | + start[i] = j |
| 176 | + break |
| 177 | + |
| 178 | + # end bound is previous end |
| 179 | + # or current index |
| 180 | + if (self.index[end[i - 1]] - end_bound) * index_growth_sign <= timedelta(0): |
| 181 | + end[i] = i + 1 |
| 182 | + else: |
| 183 | + end[i] = end[i - 1] |
| 184 | + |
| 185 | + # right endpoint is open |
| 186 | + if not right_closed: |
| 187 | + end[i] -= 1 |
| 188 | + |
| 189 | + return start, end |
| 190 | + |
| 191 | + |
107 | 192 | class ExpandingIndexer(BaseIndexer):
|
108 | 193 | """Calculate expanding window bounds, mimicking df.expanding()"""
|
109 | 194 |
|
|
0 commit comments