-1

I have like ~100 variables in my python code, Is there a way to check if anyone of the 100 variables is NaN, without having to check individual variables using isnan() ?

Mechanician
  • 525
  • 1
  • 6
  • 20
  • Does this answer your question? [How can I check for NaN values?](https://stackoverflow.com/questions/944700/how-can-i-check-for-nan-values) – rob mayoff Jul 15 '21 at 18:10
  • 3
    No human can maintain 100 variables. You should be using a dictionary, in which case checking them en masse is a piece of cake. – Tim Roberts Jul 15 '21 at 18:10
  • 2
    *"I have like ~100 variables in my python code"* Then there might be room for optimization at this point already. Consider using lists or dictionaries and iterate over them. – Klaus D. Jul 15 '21 at 18:11
  • That's a lot of variables to just coincidentally have to check all of them at the same time. Figure out how they're related, and store them in a list or dict instead of variables. – luther Jul 15 '21 at 18:15
  • 1
    Assuming that you've stored them sensibly in a list/dict but you still don't want to test them all individually, you'd need some operation that would identify 1+ values of nan. I think adding them would do this. Example: `a = [1,3,float('nan')]` and then `sum(a)` would result in nan, indicating that at least one value was nan. – jarmod Jul 15 '21 at 18:15

3 Answers3

0

Put the variables in a collection. If you always have the same set of named variables, a named tuple is appropriate. Use the any iterator to check if any of the variables is NaN.

from math import isnan
from collections import namedtuple
MyData = namedtuple('MyData', ['foo', 'bar', 'qux'])
good_data = MyData(1.0, 2.0, 3.0)
print(any(isnan(x) for x in good_data)) # False
bad_data = MyData(1.0, float('NaN'), 3.0)
any(isnan(x) for x in bad_data) # True
Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
0
import numpy as np
a=1
b=-2
c=np.nan
d=3

Variables = np.array([a, b, c, d])

SumV = np.sum(Variables)
NanExistence = np.isnan(SumV)
NanExistence

Returns:

True

This Link describes the process: https://www.kite.com/python/answers/how-to-check-for-nan-elements-in-a-numpy-array-in-python

-3

It's in the link @rob provided, but I usually use.

x != x

Not sure why you have 100 variables, but if you put them in a dict, it'd just be a matter of iterating through its values, checking each value. Otherwise, there is always eval() if you somehow have a list of all your variables.

ld = locals()
bad = []
for k, v in ld.items():
    if isinstance(v, float) and v != v:
        bad.append(k)

print("NaN Variables:", bad)
Ben Y
  • 913
  • 6
  • 18
  • 1
    The question is not "how to I check a variable for NaN". The question is "how can I check a hundred different variables without doing them one by one." – Tim Roberts Jul 15 '21 at 18:11
  • 1
    I downvoted when you added the `evil()`… sorry typo…`eval()` part. – Klaus D. Jul 15 '21 at 18:13
  • 1
    I understand the point about `eval()`, and rarely use it myself -- but if they are saying "I have a 100 variables", I can't think of any reasonable way to deal with free variables of this sort, aside from using `locals()` or `globals()`, which is as evil as `eval()`, in my opinion. Maintaining more than a dozen variables is cause for headache. – Ben Y Jul 15 '21 at 18:16