Search code examples
pythonnanmasked-array

Python masked arrays producing NaNs with np.array


I am trying to select a subset of a larger grid to perform a finite element analysis to iterate over between two arrays; a masked array and an unmasked array but i have come across a problem of NaNs appearing from the mask when using np.arrays. I originally used masks here to try to avoid NaN issues including NaN == NaN (False) etc... but the mask itself seems to be creating NaNs when used with np.arrays!

coordinate arrays to iterate over:

array = np.array(list(np.arange(5))*5).reshape(5,5)
big_array =  np.array([array,array,array])

apply masks on the coordinate arrays:

mask1 = np.ma.masked_array(big_array, mask = (big_array>2))
mask2 = np.ma.masked_array(big_array, mask = (big_array>3))

iterate over the coordinate arrays for the masked vs. unmasked comparison:

for i in range(5):
    for j in range(5):
            array_group = np.array([ big_array[0,i,j], big_array[1,i,j] ]) 
            array_group2 = np.array([ mask1[0,i,j], mask2[1,i,j] ]) 
            print array_group  

^^no NaNs from array_group but.. ^^^

        print array_group2  

NaNs appear for the masked array_group2 here


Solution

  • In your data, your mask1 outputs :

    [[[0 1 2 -- --]
      [0 1 2 -- --]
      [0 1 2 -- --]
      [0 1 2 -- --]
      [0 1 2 -- --]]
    
     [[0 1 2 -- --]
      [0 1 2 -- --]
      [0 1 2 -- --]
      [0 1 2 -- --]
      [0 1 2 -- --]]
    
     [[0 1 2 -- --]
      [0 1 2 -- --]
      [0 1 2 -- --]
      [0 1 2 -- --]
      [0 1 2 -- --]]]
    

    The little dashes are the masked data that causes the nan values to appear. As to how to fix this... I believe it depends on how you want to treat these datas : replace these dashes by 0, False, delete them... whatever.