Reputation: 2191
I was just wondering whether there is a way to query a Pandas IntervalIndex with an Interval (or range), eventually returning all Intervals in the IntervalIndex that overlap with the queried Interval.
For instance given an IntervalIndex idx
:
[1,3]
[5,8]
[10,12]
I'd like to query with a new Interval in the same domain:
new_interval = pd.Interval(2, 6, closed="both")
So something like this:
idx.get_loc(new_interval)
>> array([0, 1])
Is there a way to do this (aside from using an interval tree directly)?
Upvotes: 2
Views: 891
Reputation: 33783
Use get_indexer
:
In [1]: import pandas as pd; pd.__version__
Out[1]: '0.23.4'
In [2]: idx = pd.IntervalIndex.from_tuples([(1, 3), (5, 8), (10, 12)], closed='both')
In [3]: idx
Out[3]:
IntervalIndex([[1, 3], [5, 8], [10, 12]]
closed='both',
dtype='interval[int64]')
In [4]: new_interval = pd.Interval(2, 6, closed='both')
In [5]: idx.get_indexer([new_interval])
Out[5]: array([0, 1], dtype=int64)
Upvotes: 3