【问题标题】:In Pandas for np.nan, why is "mul()" not equal to "__mul__()"?在 np.nan 的 Pandas 中,为什么“mul()”不等于“__mul__()”?
【发布时间】:2020-09-08 13:03:45
【问题描述】:

在一个简单的 Pandas 系列中,当我比较 mul()__mul__() 的结果时,会为 NaN 值返回一个“假”布尔值。

为什么?

    x = pd.Series([0,1,2,np.nan,5])

    x.mul(1) == x.__mul__(1)

Result:
    0     True
    1     True
    2     True
    3    False
    4     True
    dtype: bool

【问题讨论】:

  • NaN 从不等于任何东西,甚至不等于它自己。这是其定义的一部分。
  • 感谢@NateEldredge。这有帮助。

标签: pandas boolean nan


【解决方案1】:

NaN 的定义属性之一是它不等于自身。见What is the rationale for all comparisons returning false for IEEE754 NaN values?

所以当x等于np.nan时,大概x.mul(1)x.__mul__(1)又都是np.nan了,测试这个值与自身是否相等的结果是假的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-03
    • 1970-01-01
    • 2010-10-15
    相关资源
    最近更新 更多