使用
python
3.10.13、pandas
2.2.0 和 numpy
1.26.4,我希望能够使用类似的东西(按照 ruff linter 的要求):
# this is False
$ (pd.array([""]) == pd.array([""]))[0] is True
False
# this does work
$ (pd.array([""]) == pd.array([""]))[0] == True
True
# though
$ (pd.array([""]) == pd.array([""]))[0]
True
注意:将
pd.array([""])
包装在列表中可以解决问题,我问为什么这里看起来 True
!= True
.
附加信息:
# you end up with a StringArray when using
$ pd.DataFrame(dict(a=[""])).unique()
# this behavior is also true for
$ (pd.DataFrame(dict(a=[""])) == pd.DataFrame(dict(a=[""]))).values[0][0] is True
False
# one needs to use pd.DataFrame.equals for it to work
$ pd.DataFrame(dict(a=[""])).equals(pd.DataFrame(dict(a=[""]))) is True
True
来自 文档 的
is
:
运算符
和is
测试对象的身份:当且仅当is not
x和 y 是同一对象时,x is y
为 true。对象的身份是使用函数确定的。id()
因此,
id
值不会相同。这是有道理的,因为尽管看起来 pd.array([""]) == pd.array([""]))[0]
不是 bool,而是 np.bool_
,即,它是一个不同的对象。
np_bool = (pd.array([""]) == pd.array([""]))[0]
id(np_bool)
# 140727655433136
type(np_bool)
numpy.bool_
id(True)
# 140727548180584
type(True)
# bool