1

pandas.Interval can be used to define if a value falls within an interval in a neat way, e.g.:

In [1]: import numpy as np

In [2]: import pandas as pd

In [3]: iv = pd.Interval(0, 5.5)

In [4]: 4.37 in iv
Out[4]: True

Is it possible to check inclusion for all elements of an array instead of a single value? The result would be the same as in:

In [5]: arr = np.array(((1,8),(-4,3.5)))

In [6]: arr
Out[6]:
array([[ 1. ,  8. ],
       [-4. ,  3.5]])

In [7]: (arr > iv.left) & (arr <= iv.right)
Out[7]:
array([[ True, False],
       [False,  True]])

But using a simpler syntax which is cool about pd.Interval. Something like the below which does not work:

In [8]: arr in iv
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-15-a118a68ee023> in <module>()
----> 1 arr in iv

pandas/_libs/interval.pyx in pandas._libs.interval.Interval.__contains__()

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

rafa
  • 235
  • 1
  • 2
  • 10
  • This is not currently available out of the box in pandas, but may get added at some point in the future (v0.25 maybe?). Not sure if 2d arrays will be supported, but 1d structures should at least be supported. – root Jan 12 '19 at 00:33

1 Answers1

0

Check with vectorize

def youf(x,iv):
    return x in iv

vfunc = np.vectorize(youf)

iv = pd.Interval(0, 5.5)

vfunc(arr, iv)
Out[27]: 
array([[ True, False],
       [False,  True]])
BENY
  • 317,841
  • 20
  • 164
  • 234