您遇到的情況實際上是一種特殊情況,它可以比較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事物並將它留在那個 –