考虑dataframe:
df = pd.DataFrame({'col': [True, False]})
下面的代码可以工作:
df['col'].diff()
结果是:
0 NaN
1 True
Name: col, dtype: object
但是,代码:
df.T.diff(axis=1)
给出错误:
numpy boolean subtract, the `-` operator, is not supported, use the bitwise_xor, the `^` operator, or the logical_xor function instead.
那是窃听器吗
2条答案
按热度按时间ctrmrzij1#
您看到的行为似乎与docs不一致,docs明确指出:
对于布尔型数据类型,使用operator.xor()而不是operator.sub()。结果根据DataFrame中的当前数据类型计算,但结果的数据类型始终为float 64。
下面的测试也很有趣:
输出:
如果我们在调用
diff(axis=1)
之前使用astype()
将列类型更改为object,则不会引发错误,并且结果显示在使用整数减法执行diff之前将布尔值转换为int。然而,正如OP所指出的,这个没有**
astype(object)
的相同操作会引发TypeErrorTypeError: numpy boolean subtract, the
-operator, is not supported, use the bitwise_xor, the
^operator, or the logical_xor function instead.
,尽管在diff()
文档中声明For boolean dtypes, this uses operator.xor() rather than operator.sub()
。vuv7lop32#
看起来这种行为是故意的,就像GH15856一样。NumPy中布尔数组之间的算术运算
(+, -, *, /, etc.)
不受支持(* 还是不再支持?)。在
axis=1
上使用diff
,pandas试图计算沿着columns轴的连续元素之间的差异( 由于换位,这里恰好包含布尔值 *),并且由于NumPy在后台运行以计算,因此引发了TypeError
。这可能是违反直觉的,因为当使用Python boolean时,相同的操作会成功:
但@seberg 解释了原因:
这是一个非常古老的反对意见,尽管我似乎记得一些关于只反对一元运算符的讨论-False而不是True - True。请注意,Python的布尔值与NumPy的不同,它们实际上是整数。NumPy布尔值的行为不像整数,如果你添加两个布尔值,你会再次得到一个布尔值,等等。