作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
pandas
的隐式索引匹配用于不同 DataFrame
之间的操作/Series
很棒,而且在大多数情况下,它只是有效。
但是,我偶然发现了一个无法按预期工作的示例:
import pandas as pd # 0.21.0
import numpy as np # 1.13.3
x = pd.Series([True, False, True, True], index = range(4))
y = pd.Series([False, True, True, False], index = [2,4,3,5])
# logical AND: this works, symmetric as it should be
pd.concat([x, y, x & y, y & x], keys = ['x', 'y', 'x&y', 'y&x'], axis = 1)
# x y x&y y&x
# 0 True NaN False False
# 1 False NaN False False
# 2 True False False False
# 3 True True True True
# 4 NaN True False False
# 5 NaN False False False
# but logical OR is not symmetric anymore (same for XOR: x^y vs. y^x)
pd.concat([x, y, x | y, y | x], keys = ['x', 'y', 'x|y', 'y|x'], axis = 1)
# x y x|y y|x
# 0 True NaN True False <-- INCONSISTENT!
# 1 False NaN False False
# 2 True False True True
# 3 True True True True
# 4 NaN True False True <-- INCONSISTENT!
# 5 NaN False False False
bool(np.nan)
等于 True
,参见https://stackoverflow.com/a/15686477/2965879 |
已解析为 np.bitwise_or
, 而不是 np.logical_or
,参见https://stackoverflow.com/a/37132854/2965879 nan
类型转换。至
False
在某一点。从上面看,这似乎是在调用
np.bitwise_or
后发生的。 ,而我认为这应该发生在之前?
np.logical_or
没有帮助,因为它错过了
pandas
的索引对齐。有,而且,我不想要
np.nan or False
等于
True
. (换句话说,答案
https://stackoverflow.com/a/37132854/2965879 没有帮助。)
|
应该是对称的 .当总是对称的东西突然不再存在时,真的很难调试(就像我发生的那样)。
x|y == y|x
,理想情况下,(粗略地说)
nan | True == True == True | nan
和
nan | False == False == False | nan
?
~(x&y)
不能完全匹配
~y|~x
因为 NaN 仅在索引对齐时出现(因此不受先前否定的影响)。
最佳答案
在pandas中做了一些探索之后,我发现有一个函数叫pandas.core.ops._bool_method_SERIES
这是为 Series 对象包装 bool 运算符的几个工厂函数之一。
>>> f = pandas.Series.__or__
>>> f #the actual function you call when you do x|y
<function _bool_method_SERIES.<locals>.wrapper at 0x107436bf8>
>>> f.__closure__[0].cell_contents
#it holds a reference to the other function defined in this factory na_op
<function _bool_method_SERIES.<locals>.na_op at 0x107436b70>
>>> f.__closure__[0].cell_contents.__closure__[0].cell_contents
#and na_op has a reference to the built-in function or_
<built-in function or_>
def test_logical_or(a,b):
print("**** calling logical_or with ****")
print(type(a), a)
print(type(b), b)
print("******")
raise TypeError("my_logical_or isn't implemented")
#make the wrapper method
wrapper = pd.core.ops._bool_method_SERIES(test_logical_or, None,None)
pd.Series.logical_or = wrapper #insert method
x = pd.Series([True, False, True, True], index = range(4))
y = pd.Series([False, True, True, False], index = [2,4,3,5])
z = x.logical_or(y) #lets try it out!
print(x,y,z, sep="\n")
**** calling logical_or with ****
<class 'numpy.ndarray'> [True False True True nan nan]
<class 'numpy.ndarray'> [False False False True True False]
******
**** calling logical_or with ****
<class 'bool'> True
<class 'bool'> False
******
Traceback (most recent call last):
...
nan
值已替换为
False
但不是第一个,这可能是我们的对称性破裂的原因。然后当失败时它再次尝试我会假设元素明智。
nan
第一个到
False
的条目然后
return np.logical_or(a,b)
.我将假设如果有任何其他情况,我们只会引发错误。
def my_logical_or(a,b):
if isinstance(a, np.ndarray) and isinstance(b, np.ndarray):
a[np.isnan(a.astype(float))] = False
b[np.isnan(b.astype(float))] = False
return np.logical_or(a,b)
else:
raise TypeError("custom logical or is only implemented for numpy arrays")
wrapper = pd.core.ops._bool_method_SERIES(my_logical_or, None,None)
pd.Series.logical_or = wrapper
x = pd.Series([True, False, True, True], index = range(4))
y = pd.Series([False, True, True, False], index = [2,4,3,5])
z = pd.concat([x, y, x.logical_or(y), y.logical_or(x)], keys = ['x', 'y', 'x|y', 'y|x'], axis = 1)
print(z)
# x y x|y y|x
# 0 True NaN True True
# 1 False NaN False False <-- same!
# 2 True False True True
# 3 True True True True
# 4 NaN True True True <-- same!
# 5 NaN False False False
Series.__or__
因为我们不知道还有谁会使用它,并且不想破坏任何期望默认行为的代码。
pandas.core.ops
line 943 处修改源代码填写
NaN
self
的值为 False(或 0)在同一个
way it does with other
,所以我们会改变这一行:
return filler(self._constructor(na_op(self.values, other.values),
index=self.index, name=name))
filler(self).values
而不是
self.values
:
return filler(self._constructor(na_op(filler(self).values, other.values),
index=self.index, name=name))
or
的问题和
xor
不是对称的,但是,我不建议这样做,因为它可能会破坏其他代码,我个人对 Pandas 没有足够的经验来确定这在不同情况下会发生什么变化。
关于python-3.x - bool pandas.Series 之间的操作对称性不等,索引不等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47659400/
我正在使用 Ruby 加密并使用 AES-256 通过 JavaScript 解密,但我在跨平台时遇到问题,即在解密 Ruby 的输出时 JS 返回乱码。 我正在使用此处的 JS AES 实现:htt
我是一名优秀的程序员,十分优秀!