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()
答案 0 :(得分:0)
使用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]])