Is there a neat way to check for the inclusion in an interval of all values in an array using pandas (or another python tool)?

462 views Asked by At

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()

1

There are 1 answers

0
BENY On

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]])