是否可以将数字设置为NaN或无穷大?

Bob*_*Bob 191 python numeric nan infinite

是否可以NaN在Python中设置数组元素?

另外,是否可以将变量设置为+/-无穷大?如果是,是否有任何功能来检查数字是否无穷大?

mar*_*cog 249

从字符串转换使用float():

>>> float('NaN')
nan
>>> float('Inf')
inf
>>> -float('Inf')
-inf
>>> float('Inf') == float('Inf')
True
>>> float('Inf') == 1
False
Run Code Online (Sandbox Code Playgroud)

  • 这将教会我在第二次阅读这个问题之前不要用俏皮话插话!!对不起!也就是说,这样说也没什么坏处,因为这是一个很容易陷入的陷阱,NaN != NaN (2认同)
  • 还请注意:>>> float('Inf')-float('Inf')===> nan (2认同)

Oli*_*ier 75

是的,你可以用numpy它.

import numpy as np
a = arange(3,dtype=float)

a[0] = np.nan
a[1] = np.inf
a[2] = -np.inf

a # is now [nan,inf,-inf]

np.isnan(a[0]) # True
np.isinf(a[1]) # True
np.isinf(a[2]) # True
Run Code Online (Sandbox Code Playgroud)

  • 在python> = 2.6上,你可以使用`math.isnan()`和`math.isinf()` (21认同)
  • 如果你想要的只是"NaN"或"inf",那么"numpy"是一个非常重要的导入 (7认同)
  • 如果您只需要“NaN”或“Inf”,则可以从“from numpy import nan, inf” 提出这个问题以来一直存在。 (2认同)

MSe*_*ert 34

是否可以将数字设置为NaN或无穷大?

是的,实际上有几种方法.一些没有任何导入的工作,而其他人需要import,但是对于这个答案,我将概述中的库限制为标准库和NumPy(这不是标准库,而是非常常见的第​​三方库).

下表总结了如何创建非数字或正无穷大的方式float:

?????????????????????????????????????????????????????????????????????
?   result ? NaN          ? Infinity           ? -Infinity          ?
? module   ?              ?                    ?                    ?
?????????????????????????????????????????????????????????????????????
? built-in ? float("nan") ? float("inf")       ? -float("inf")      ?
?          ?              ? float("infinity")  ? -float("infinity") ?
?          ?              ? float("+inf")      ? float("-inf")      ?
?          ?              ? float("+infinity") ? float("-infinity") ?
?????????????????????????????????????????????????????????????????????
? math     ? math.nan     ? math.inf           ? -math.inf          ?
?????????????????????????????????????????????????????????????????????
? cmath    ? cmath.nan    ? cmath.inf          ? -cmath.inf         ?
?????????????????????????????????????????????????????????????????????
? numpy    ? numpy.nan    ? numpy.PINF         ? numpy.NINF         ?
?          ? numpy.NaN    ? numpy.inf          ? -numpy.inf         ?
?          ? numpy.NAN    ? numpy.infty        ? -numpy.infty       ?
?          ?              ? numpy.Inf          ? -numpy.Inf         ?
?          ?              ? numpy.Infinity     ? -numpy.Infinity    ?
?????????????????????????????????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)

几个评论表:

  • float构造函数实际上是不区分大小写的,所以你也可以使用float("NaN")float("InFiNiTy").
  • cmathnumpy常量返回普通的Python float对象.
  • numpy.NINF实际上是我所知道的唯一不需要的常数-.
  • 可以使用complex和创建复杂的NaN和Infinity cmath:

    ????????????????????????????????????????????????????????????????????????????????????????????
    ?   result ? NaN+0j         ? 0+NaNj          ? Inf+0j              ? 0+Infj               ?
    ? module   ?                ?                 ?                     ?                      ?
    ????????????????????????????????????????????????????????????????????????????????????????????
    ? built-in ? complex("nan") ? complex("nanj") ? complex("inf")      ? complex("infj")      ?
    ?          ?                ?                 ? complex("infinity") ? complex("infinityj") ?
    ????????????????????????????????????????????????????????????????????????????????????????????
    ? cmath    ? cmath.nan ¹    ? cmath.nanj      ? cmath.inf ¹         ? cmath.infj           ?
    ????????????????????????????????????????????????????????????????????????????????????????????
    
    Run Code Online (Sandbox Code Playgroud)

是否有任何功能来检查数字是否是无穷大?

是的 - 事实上,NaN,Infinity以及Nan和Inf都有几个功能.但是,这些预定义的函数不是内置的,它们总是需要float:

????????????????????????????????????????????????????????????????
?      for ? NaN         ? Infinity or    ? not NaN and        ?
?          ?             ? -Infinity      ? not Infinity and   ?
? module   ?             ?                ? not -Infinity      ?
????????????????????????????????????????????????????????????????
? math     ? math.isnan  ? math.isinf     ? math.isfinite      ?
????????????????????????????????????????????????????????????????
? cmath    ? cmath.isnan ? cmath.isinf    ? cmath.isfinite     ?
????????????????????????????????????????????????????????????????
? numpy    ? numpy.isnan ? numpy.isinf    ? numpy.isfinite     ?
????????????????????????????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)

再说几句话:

  • compleximport功能也工作了复杂的对象,他们会检查是否真实或虚部是NAN或无穷.
  • 这些cmath函数也适用于numpy数组和可以转换为一个的所有东西(如列表,元组等)
  • 在NumPy中还有明确检查正负无穷大的函数:numpynumpy.
  • 熊猫提供了两个额外的功能检查numpy.isposinf(但不是唯一的NaN,它也匹配numpy.isneginfNaN)
  • 即使没有内置函数,也很容易自己创建它们(我忽略了类型检查和文档):

    def isnan(value):
        return value != value  # NaN is not equal to anything, not even itself
    
    infinity = float("infinity")
    
    def isinf(value):
        return abs(value) == infinity 
    
    def isfinite(value):
        return not (isnan(value) or isinf(value))
    
    Run Code Online (Sandbox Code Playgroud)

总结这些函数的预期结果(假设输入是浮点数):

????????????????????????????????????????????????????????????????????????
?          input ? NaN   ? Infinity   ? -Infinity   ? something else   ?
? function       ?       ?            ?             ?                  ?
????????????????????????????????????????????????????????????????????????
? isnan          ? True  ? False      ? False       ? False            ?
????????????????????????????????????????????????????????????????????????
? isinf          ? False ? True       ? True        ? False            ?
????????????????????????????????????????????????????????????????????????
? isfinite       ? False ? False      ? False       ? True             ?
????????????????????????????????????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)

是否可以在Python中将数组元素设置为NaN?

在列表中没有问题,你可以在那里包含NaN(或Infinity):

>>> [math.nan, math.inf, -math.inf, 1]  # python list
[nan, inf, -inf, 1]
Run Code Online (Sandbox Code Playgroud)

但是如果你想要将其包含在pandas.isna(例如pandas.isnullNone),则数组的类型必须NaTarray,否则它会尝试将其向下到阵列的类型!

>>> import numpy as np
>>> float_numpy_array = np.array([0., 0., 0.], dtype=float)
>>> float_numpy_array[0] = float("nan")
>>> float_numpy_array
array([nan,  0.,  0.])

>>> import array
>>> float_array = array.array('d', [0, 0, 0])
>>> float_array[0] = float("nan")
>>> float_array
array('d', [nan, 0.0, 0.0])

>>> integer_numpy_array = np.array([0, 0, 0], dtype=int)
>>> integer_numpy_array[0] = float("nan")
ValueError: cannot convert float NaN to integer
Run Code Online (Sandbox Code Playgroud)