|
| 1 | +""" |
| 2 | +Numba 1D sum kernels that can be shared by |
| 3 | +* Dataframe / Series |
| 4 | +* groupby |
| 5 | +* rolling / expanding |
| 6 | +
|
| 7 | +Mirrors pandas/_libs/window/aggregation.pyx |
| 8 | +""" |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import numba |
| 12 | +import numpy as np |
| 13 | + |
| 14 | +from pandas.core._numba.kernels.shared import is_monotonic_increasing |
| 15 | + |
| 16 | + |
| 17 | +@numba.jit(nopython=True, nogil=True, parallel=False) |
| 18 | +def add_sum( |
| 19 | + val: float, nobs: int, sum_x: float, compensation: float |
| 20 | +) -> tuple[int, float, float]: |
| 21 | + if not np.isnan(val): |
| 22 | + nobs += 1 |
| 23 | + y = val - compensation |
| 24 | + t = sum_x + y |
| 25 | + compensation = t - sum_x - y |
| 26 | + sum_x = t |
| 27 | + return nobs, sum_x, compensation |
| 28 | + |
| 29 | + |
| 30 | +@numba.jit(nopython=True, nogil=True, parallel=False) |
| 31 | +def remove_sum( |
| 32 | + val: float, nobs: int, sum_x: float, compensation: float |
| 33 | +) -> tuple[int, float, float]: |
| 34 | + if not np.isnan(val): |
| 35 | + nobs -= 1 |
| 36 | + y = -val - compensation |
| 37 | + t = sum_x + y |
| 38 | + compensation = t - sum_x - y |
| 39 | + sum_x = t |
| 40 | + return nobs, sum_x, compensation |
| 41 | + |
| 42 | + |
| 43 | +@numba.jit(nopython=True, nogil=True, parallel=False) |
| 44 | +def sliding_sum( |
| 45 | + values: np.ndarray, |
| 46 | + start: np.ndarray, |
| 47 | + end: np.ndarray, |
| 48 | + min_periods: int, |
| 49 | +) -> np.ndarray: |
| 50 | + N = len(start) |
| 51 | + nobs = 0 |
| 52 | + sum_x = 0.0 |
| 53 | + compensation_add = 0.0 |
| 54 | + compensation_remove = 0.0 |
| 55 | + |
| 56 | + is_monotonic_increasing_bounds = is_monotonic_increasing( |
| 57 | + start |
| 58 | + ) and is_monotonic_increasing(end) |
| 59 | + |
| 60 | + output = np.empty(N, dtype=np.float64) |
| 61 | + |
| 62 | + for i in range(N): |
| 63 | + s = start[i] |
| 64 | + e = end[i] |
| 65 | + if i == 0 or not is_monotonic_increasing_bounds: |
| 66 | + for j in range(s, e): |
| 67 | + val = values[j] |
| 68 | + nobs, sum_x, compensation_add = add_sum( |
| 69 | + val, nobs, sum_x, compensation_add |
| 70 | + ) |
| 71 | + else: |
| 72 | + for j in range(start[i - 1], s): |
| 73 | + val = values[j] |
| 74 | + nobs, sum_x, compensation_remove = remove_sum( |
| 75 | + val, nobs, sum_x, compensation_remove |
| 76 | + ) |
| 77 | + |
| 78 | + for j in range(end[i - 1], e): |
| 79 | + val = values[j] |
| 80 | + nobs, sum_x, compensation_add = add_sum( |
| 81 | + val, nobs, sum_x, compensation_add |
| 82 | + ) |
| 83 | + |
| 84 | + if nobs == 0 == nobs: |
| 85 | + result = 0.0 |
| 86 | + elif nobs >= min_periods: |
| 87 | + result = sum_x |
| 88 | + else: |
| 89 | + result = np.nan |
| 90 | + |
| 91 | + output[i] = result |
| 92 | + |
| 93 | + if not is_monotonic_increasing_bounds: |
| 94 | + nobs = 0 |
| 95 | + sum_x = 0.0 |
| 96 | + compensation_remove = 0.0 |
| 97 | + |
| 98 | + return output |
0 commit comments