您遇到的情况实际上是一种特殊情况,它可以比较pandas.Series
或numpy.ndarray
与普通的python结构。源代码如下:
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
因为熊猫承担.eq
,你想转换为而不是(如果你给它一个数组,列表或元组)numpy.ndarray
或pandas.Series
值实际上将其与tuple
进行比较。例如,如果您有:
s = pd.Series([1,2,3])
s.eq([1,2,3])
你不希望它的每个元素比较[1,2,3]
。
问题是object
阵列(与dtype=uint
一样)经常滑过裂纹或被故意忽略。一个简单的if self.dtype != 'object'
分支内的那个方法可以解决这个问题。但也许开发商有充分的理由让这个案例变得不同。我建议通过张贴在他们的bug tracker上来要求澄清。
你还没有问你如何使它正常工作,但completness我会包括一个可能性(根据源代码,它很可能你需要用它作为自己pandas.Series
):
>>> s.eq(pd.Series([(1, 2)]))
0 True
1 False
2 False
dtype: bool
@piRSquared:现在你已经在帖子中增加了一些研究成果的证据!起初你只是说过你已经假定了X事物并将它留在那个 –