# How to check for float(‘nan’) in Python?

We Are Going To Discuss About How to check for float(‘nan’) in Python?. So lets Start this Python Article.

## How to check for float(‘nan’) in Python?

1. How to solve How to check for float('nan') in Python?

It's very pedestrian, and a bit ugly, but why not just do the following?
`import math import numpy as np if math.isnan(x) or x in ['nan', np.nan, ... ]: # Do something pass`
I want to recommend a Yoda expression but haven't quite worked it out yet.
If you want to sweep everything under the carpet put it in a lambda or function.
Following on from https://stackoverflow.com/a/64090763/1021819, you can try to get the iterator to evaluate any in a lazy fashion. The problem then is that if none of the first conditions evaluates to `True` then the `math.isnan()` call is executed and can still throw the `TypeError`. If you evaluate lazily you can guard the `math.isnan()` call with a type check against `str`:
`fn_list_to_check=[ lambda x: x in ['nan', np.nan, ... ], lambda x: not isinstance(x, str), lambda x: math.isnan(x) ] if any(f(x) for f in fn_list_to_check): # Do something pass`
Note the absence of square list brackets in the any i.e. `any()` not `any([])` (who knew?).
I think it's quite brilliant but equally as ugly – choose your poison.
For the second part of the question (why `float('nan') != float('nan')`), see
What is the rationale for all comparisons returning false for IEEE754 NaN values?

2. How to check for float('nan') in Python?

It's very pedestrian, and a bit ugly, but why not just do the following?
`import math import numpy as np if math.isnan(x) or x in ['nan', np.nan, ... ]: # Do something pass`
I want to recommend a Yoda expression but haven't quite worked it out yet.
If you want to sweep everything under the carpet put it in a lambda or function.
Following on from https://stackoverflow.com/a/64090763/1021819, you can try to get the iterator to evaluate any in a lazy fashion. The problem then is that if none of the first conditions evaluates to `True` then the `math.isnan()` call is executed and can still throw the `TypeError`. If you evaluate lazily you can guard the `math.isnan()` call with a type check against `str`:
`fn_list_to_check=[ lambda x: x in ['nan', np.nan, ... ], lambda x: not isinstance(x, str), lambda x: math.isnan(x) ] if any(f(x) for f in fn_list_to_check): # Do something pass`
Note the absence of square list brackets in the any i.e. `any()` not `any([])` (who knew?).
I think it's quite brilliant but equally as ugly – choose your poison.
For the second part of the question (why `float('nan') != float('nan')`), see
What is the rationale for all comparisons returning false for IEEE754 NaN values?

## Solution 1

It’s very pedestrian, and a bit ugly, but why not just do the following?

``````import math
import numpy as np

if math.isnan(x) or x in ['nan', np.nan, ... ]:
# Do something
pass
``````

I want to recommend a Yoda expression but haven’t quite worked it out yet.

If you want to sweep everything under the carpet put it in a lambda or function.

Following on from https://stackoverflow.com/a/64090763/1021819, you can try to get the iterator to evaluate any in a lazy fashion. The problem then is that if none of the first conditions evaluates to `True` then the `math.isnan()` call is executed and can still throw the `TypeError`. If you evaluate lazily you can guard the `math.isnan()` call with a type check against `str`:

``````fn_list_to_check=[
lambda x: x in ['nan', np.nan, ... ],
lambda x: not isinstance(x, str),
lambda x: math.isnan(x)
]

if any(f(x) for f in fn_list_to_check):
# Do something
pass

``````

Note the absence of square list brackets in the any i.e. `any()` not `any([])` (who knew?).

I think it’s quite brilliant but equally as ugly – choose your poison.

For the second part of the question (why `float('nan') != float('nan')`), see

What is the rationale for all comparisons returning false for IEEE754 NaN values?

Original Author jtlz2 Of This Content

## Solution 2

Why not just wrap whatever you pass to `math.isnan` with another `float` conversion? If it was already a float (i.e. `float('nan')`) you just made a “redundant” call:

``````import math

def is_nan(value):
return math.isnan(float(value))
``````

And this seems to give your expected results:

``````>>> is_nan(float('nan'))
True
>>> is_nan('nan')
True
>>> is_nan(np.nan)
True
``````

Original Author Tomerikoo Of This Content

## Solution 3

You can check for NaN value like this,

`````` def isNaN(num):
if num == 'nan':
return True
return num!= num
``````

Original Author Gautam Jangid Of This Content

## Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You. 