实际上,您遇到的是一种特殊情况,可以更轻松地与常规python结构进行比较
pandas.Series或比较
numpy.ndarray。源代码为:
def flex_wrapper(self, other, level=None, fill_value=None, axis=0): # validate axis if axis is not None: self._get_axis_number(axis) if isinstance(other, ABCSeries): return self._binop(other, op, level=level, fill_value=fill_value) elif isinstance(other, (np.ndarray, list, tuple)): if len(other) != len(self): # --------------------------------------- # you never reach the `==` path because you get into this. # --------------------------------------- raise ValueError('Lengths must be equal') return self._binop(self._constructor(other, self.index), op, level=level, fill_value=fill_value) else: if fill_value is not None: self = self.fillna(fill_value) return self._constructor(op(self, other),self.index).__finalize__(self)您之所以会碰上,是
ValueError因为pandas假设
.eq您希望将值转换为
numpy.ndarrayor
pandas.Series(
如果您给它一个数组,列表或元组 ),而不是实际将其与进行比较
tuple。例如,如果您有:
s = pd.Series([1,2,3])s.eq([1,2,3])
您不希望它将每个元素与进行比较
[1,2,3]。
问题在于,
object阵列(与一样
dtype=uint)经常会滑过裂缝或被故意忽略。
if self.dtype !='object'该方法中的一个简单分支 可以
解决此问题。但是,也许开发人员有充分的理由使这种情况有所不同。我建议通过在他们的错误跟踪器上发布进行澄清。
您没有询问如何使其正常工作,但是出于完整性的考虑,我将提供一种可能性(根据源代码,似乎您需要将其包装为
pandas.Series自己):
>>> s.eq(pd.Series([(1, 2)]))0 True1 False2 Falsedtype: bool



