Description
Problem description
During my work on generalizing our rolling window functions, @mroeschke and @jreback have pointed out that it would be useful to expose a couple BaseIndexer
subclasses as a part of our api (in pandas.api.indexers, to be specific). For instance, forward-looking windows are fairly common in signal analysis and the like, and it would benefit users if we had a default implementation of those.
This is the implementation I'm currently using for testing:
class ForwardIndexer(BaseIndexer):
def get_window_bounds(self, num_values, min_periods, center, closed):
start = np.empty(num_values, dtype=np.int64)
end = np.empty(num_values, dtype=np.int64)
for i in range(num_values):
if i + min_periods <= num_values:
start[i] = i
end[i] = min(i + self.window_size, num_values)
else:
start[i] = i
end[i] = i + 1
return start, end
This should be rewritten to avoid the for loop, as it slows the function down a lot.
We could also add a backward indexer for completeness or skip it, because it can be done by supplying an integer when creating the rolling window object. Tell me what you think.
I'd be happy to submit a PR with a proposed implementation in a day or two.
Implementation details
I think we can put the implementation of the new class/classes in pandas.core.window.indexers
.