Using float ('nan') to represent missing values ​​- is it safe?

Python 3.1

I am doing some calculations on data that has no values. Any calculations that include a missing value should result in a missing value.

I think use float('nan')to represent missing values. It is safe? In the end, I just check

def is_missing(x):
  return x!=x # I hope it safe to use to check for NaN

It seems perfect, but I could not find a clear confirmation in the documentation.

I could use “No,” of course, but that would require me to do each calculation with try/ except TypeErrorto detect it. I could also use Inf, but I'm even less sure about how this works.

EDIT:

@MSN I understand that using NaN is slow. But if my choice is:

# missing value represented by NaN
def f(a, b, c):
  return a + b * c

or

# missing value represented by None
def f(a, b, c):
  if a is None or b is None or c is None:
    return None
  else:
    return a + b * c

, NaN , ?

+3
1

, FPU - x, ( NaN ): INE754 NaN , ?

+1

Source: https://habr.com/ru/post/1772160/


All Articles