0

These are the tries I have made:

series1 = np.array([1, 3, None, None, 5]).astype(np.float)
print(series1)
for i in range(series1.shape[0]):
    if series1[i] == None:
        print(series1[i])
    if series1[i] == 'None':
        print(i)
    try:
        if series1[i] == nan:
            print(series1[i])
    except NameError:
        pass
    if series1[i] == 'nan':
        print(series1[i])

gives output

[ 1.  3. nan nan  5.]

I need the if statements to identify where the elements in the Numpy array is None. But I can't think of anything other than what's in the above code. It works when I don't provide the .astype(np.float) command, it prints out None, but I need the elements to be float

lAPPYc
  • 1
  • 3
  • I think what you are looking for can be found here https://stackoverflow.com/questions/37754948/how-to-get-the-indices-list-of-all-nan-value-in-numpy-array – eandklahn Apr 12 '21 at 12:58
  • `x is None` is the preferred test for `None` – hpaulj Apr 12 '21 at 15:04

1 Answers1

0

TL;DR Use

import math
...
   if math.isnan(series1[i]):
      ...

nan is a special floating point value, signalizing that number is not a valid value- it means Not a Number. It is distinct from None, which is a Python object - which is not a float, or a numeric value.

Numpy will convert None to nans when creating arrays, and that is ok - their semantic meaning is the same. Besides conversion from None, certain arithmetic results can also result in NaN's such as divisions of infinity by infinity, or in array operations where raising an exception in a zero division or other invalid op would break all the program flow.

However, nans can only be detected in certain specialized ways. Comparison is not one of them - it is the only regular object in Python which compares different from itself with the == comparison. (That is: nan == nan results in False).

Then, you are trying to compare yur float valus with 'None' and 'nan': quoted in this way these are strings of text - the characters composing the name, not the special Python objects.

The correct way to check for nan in regular Python is by using math.isnan(value) (you have to import math prior to doing that). It returns True or False, and can be used directly in the if statement.

If at some point you are using NumPy's vector functions, instead of using an external Python for, there is the numpy.isnan call.

jsbueno
  • 99,910
  • 10
  • 151
  • 209