pandas.Interval 可用于定义一个值是否以简洁的方式落在一个区间内,例如:
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
是否可以检查包含数组的所有元素而不是单个值?结果将与以下相同:
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]])
但是使用更简单的语法,这对 pd.Interval 来说很酷。像下面这样不起作用的东西:
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()