def sparse_mov_avg(idx, val, win):
    avg_idx = np.arange(-win, win + 1) + idx[:, (None)]
avg_val = np.tile(val[:, (None)] / (2 * win + 1), (1, 2 * win + 1))
unique_idx, _ = np.unique(avg_idx, return_inverse=True)
mov_avg = np.bincount(_, weights=avg_val.ravel())
return unique_idx, mov_avg
Efficiently take moving average of sparse data and filter above threshold in python