There's this interesting API called Intervalindex
new in 0.20 that lets you create an index of intervals.
Given some sample data:
data = [(893.1516130000001, 903.9187099999999),
(882.384516, 893.1516130000001),
(817.781935, 828.549032)]
You can create the index like this:
idx = pd.IntervalIndex.from_tuples(data)
print(idx)
IntervalIndex([(893.151613, 903.91871], (882.384516, 893.151613], (817.781935, 828.549032]]
closed='right',
dtype='interval[float64]')
An interesting property of Interval
s is that you can perform interval checks with in
:
print(y[-1])
Interval(817.78193499999998, 828.54903200000001, closed='right')
print(820 in y[-1])
True
print(1000 in y[-1])
False
I would like to know how to apply this operation to the entire index. For example, given some number 900
, how could I retrieve a boolean mask of intervals for which this number fits in?
I can think of:
m = [900 in y for y in idx]
print(m)
[True, False, False]
Are there better ways to do this?