Reputation: 2813
Is there a nicer way to check if pandas Series values are in pandas Interval:
import pandas as pd, numpy as np
x = pd.Series(np.linspace(4.0,7.8,num=20))
i = pd.Interval(5.0, 6.0, closed='left')
result = (i.left<=x) & (x<i.right)
Is it possible to compute result less explicitly, i.e. without accessing i.left
, i.right
, i.closed
?
Something like x.isin(i)
or x in i
.
Thank you for your help!
Upvotes: 5
Views: 1695
Reputation: 887
You can use pandas.cut()
for that:
bins=[5.0,6.0]
pd.cut(x,bins,right=False).dropna()
5 [5.0, 6.0)
6 [5.0, 6.0)
7 [5.0, 6.0)
8 [5.0, 6.0)
9 [5.0, 6.0)
Upvotes: -1