Joffrey Schmitz
Joffrey Schmitz

Reputation: 2438

Numpy : check for integer NaN

I need to have a table with columns of different datatypes (float or integer).

I use the dtype to define them :

import numpy as np

# define array
datadef=[ ('i', '<i4'), ('f', '<f8'), ('g', '<f8'), ('j', '<i4') ]
arr = np.full((4,), np.nan, dtype=datadef) 

# fill array with data
arr['i'] = np.array([1, 2, 3, 4])
arr['f'] = np.array([1.3333333333, np.nan, 2.6666666666666666, 5.0])
arr['g'] = np.array([2.77777777777, 5.4, 3.4, np.nan])
# nothing for 'j'
print arr

Output :

[(1,  1.33333333,  2.77777778, -2147483648)
 (2,         nan,  5.4       , -2147483648)
 (3,  2.66666667,  3.4       , -2147483648)
 (4,  5.        ,         nan, -2147483648)]

The NaN values of the last column has been transformed into -2147483648, no problem with that so far.

But now I am not able to check if the values are indeed NaN in my array :

row = arr[1]
print np.isnan(row) # TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

And on a single cell it seems like the NaN information is lost and the -2147483648 is considered as a "classic number" :

print row # (2,  nan,  5.4, -2147483648)
print np.isnan(row[0]) # False, OK
print np.isnan(row[1]) # True, OK
print np.isnan(row[3]) # False, expected True

Is there an easy way to check for NaN on integers in this situation ?

Upvotes: 0

Views: 1562

Answers (1)

Ehsan
Ehsan

Reputation: 12397

np.nan is float and not an integer. You either have to change your dtype for last column or use a different structure to store your nan as integer.

datadef=[ ('i', '<i4'), ('f', '<f8'), ('g', '<f8'), ('j', '<f4') ]
arr = np.full((4,), np.nan, dtype=datadef) 

# fill array with data
arr['i'] = np.array([1, 2, 3, 4])
arr['f'] = np.array([1.3333333333, np.nan, 2.6666666666666666, 5.0])
arr['g'] = np.array([2.77777777777, 5.4, 3.4, np.nan])
# nothing for 'j'

Now try printing np.isnan statement:

print(np.isnan(arr[1][3]))

True

Upvotes: 1

Related Questions