Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么不是';逻辑运算符是否应该在索引上对齐?_Python_Pandas - Fatal编程技术网

Python 为什么不是';逻辑运算符是否应该在索引上对齐?

Python 为什么不是';逻辑运算符是否应该在索引上对齐?,python,pandas,Python,Pandas,考虑以下简单设置: x = pd.Series([1, 2, 3], index=list('abc')) y = pd.Series([2, 3, 3], index=list('bca')) x a 1 b 2 c 3 dtype: int64 y b 2 c 3 a 3 dtype: int64 正如您所看到的,索引是相同的,只是顺序不同 现在,使用等式(=< /代码>)操作符考虑一个简单的逻辑比较: x == y --------------

考虑以下简单设置:

x = pd.Series([1, 2, 3], index=list('abc'))
y = pd.Series([2, 3, 3], index=list('bca'))

x

a    1
b    2
c    3
dtype: int64

y

b    2
c    3
a    3
dtype: int64
正如您所看到的,索引是相同的,只是顺序不同

现在,使用等式(<代码>=< /代码>)操作符考虑一个简单的逻辑比较:

x == y
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
这会抛出一个
ValueError
,很可能是因为索引不匹配。另一方面,调用等效的
eq
操作符可以:

x.eq(y)

a    False
b     True
c     True
dtype: bool
OTOH,运算符方法在给定
y
的情况下工作,首先重新排序

x == y.reindex_like(x)

a    False
b     True
c     True
dtype: bool

我的理解是,函数和运算符的比较应该做同样的事情,所有其他事情都是一样的。什么是
eq
做的而运算符比较没有做的

我喜欢python的一点是,几乎任何东西的源代码都可以达到顶峰。从
pd.Series.eq
源代码,它调用:

def flex_wrapper(self, other, level=None, fill_value=None, axis=0):
    # other stuff
    # ...

    if isinstance(other, ABCSeries):
        return self._binop(other, op, level=level, fill_value=fill_value)
然后转到pd.Series.\u binop:

def _binop(self, other, func, level=None, fill_value=None):

    # other stuff
    # ...
    if not self.index.equals(other.index):
        this, other = self.align(other, level=level, join='outer',
                                 copy=False)
        new_index = this.index

这意味着
eq
操作符在比较之前将两个序列对齐(显然,正常操作符
=
没有对齐)。

回到2012年,那时我们没有
eq
ne
gt
pandas
有问题:disorder
Series
将返回带有逻辑的意外输出(
,查看整个回溯,以进行索引不匹配的序列比较,尤其关注异常消息:

In [1]: import pandas as pd
In [2]: x = pd.Series([1, 2, 3], index=list('abc'))
In [3]: y = pd.Series([2, 3, 3], index=list('bca'))
In [4]: x == y
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-73b2790c1e5e> in <module>()
----> 1 x == y
/usr/lib/python3.7/site-packages/pandas/core/ops.py in wrapper(self, other, axis)
   1188 
   1189         elif isinstance(other, ABCSeries) and not self._indexed_same(othe
r):
-> 1190             raise ValueError("Can only compare identically-labeled "
   1191                              "Series objects")
   1192 
ValueError: Can only compare identically-labeled Series objects
[1]中的
:将熊猫作为pd导入
在[2]中:x=pd.Series([1,2,3],index=list('abc'))
在[3]中:y=pd.Series([2,3,3],index=list('bca'))
在[4]中:x==y
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在()
---->1 x==y
/包装器中的usr/lib/python3.7/site-packages/pandas/core/ops.py(self、other、axis)
1188
1189 elif isinstance(其他,ABC系列)和非self.\u indexed\u same(其他
r) :
->1190 raise VALUE ERROR(“只能比较标签相同的项目”
1191“系列对象”)
1192
ValueError:只能比较标签相同的系列对象
我们看到这是一个经过深思熟虑的实现决策。而且,这并不是系列对象所独有的—数据帧会引发类似的错误

挖掘Git对相关行的指责最终会发现一些相关的提交和问题跟踪线程。例如,
系列。过去完全忽略RHS的索引,在关于该行为的错误报告中,熊猫作者Wes McKinney说:

这实际上是一个特性/深思熟虑的选择,而不是一个bug——它是 与相关。早在一月份,我将比较方法更改为 做自动校准,但发现它导致了大量的错误/ 对于用户,尤其是许多NumPy函数( 定期做一些事情,比如
arr[1::][=arr[:-1]
;例如:
np.unique
) 停止工作

这又回到了那个系列剧不太像的问题上 足够了,可能不应该是ndarray的子类

所以,除了这个,我没有给你一个好的答案; 自动校准将是理想的,但我不认为我能做到,除非我 使系列不是ndarray的子类。我认为这可能是一个好方法 想法,但在0.9或0.10之前不太可能发生(几个月后 道路)

这就是熊猫0.19.0中的当前行为。引用:

以下系列运算符已更改为所有运算符 一致,包括数据帧(, )

  • 序列比较运算符现在在索引不同时引发ValueError
  • 系列逻辑运算符将左侧和右侧的索引对齐
这使得序列行为与DataFrame相匹配,DataFrame已经在比较中拒绝了不匹配的索引


总而言之,让比较运算符自动对齐索引会破坏太多内容,因此这是最好的选择。

不同的问题,但只是为了网络连接:很好的答案。应该有一个调查员徽章。专为这样的答案设计,答案作者显然花了时间进行研究,阅读代码,通过Git查找逻辑解释。+1