views:

126

answers:

2

Alright, extreme rookie question here. In my program, I generate a 2D numpy array, some of whom's entries are missing (not the "nan" kind of nonexistant, but the "None" kind, or NoneType). I'd like to put a mask over these entries, but I seem to be having some trouble doing so. Ordinarily, to mask over, say, all entries with value 2, I'd do

A = np.ma.masked_where(A[A==2], A)

In this case, that doesn't seem to work no matter what I try for the first parameter. Thoughts?

+3  A: 

Since you have -- entries in your array, I guess that it means that they are already masked:

>>> m = ma.masked_where([True, False]*5, arange(10))
>>> print m
[-- 1 -- 3 -- 5 -- 7 -- 9]

So, I would say that your entries are already masked and that you can directly use your array.

If you want to create an array that only contains the non-masked value, you can do

>>> m[~m.mask]
[1 3 5 7]

where m is your masked array.

If you want to have the list of masked values, you can simply select the other values:

>>> m[m.mask]
[0 2 4 6 8]

Note that the missing values are not None, but are the original values, generally. In fact, an array of integers cannot contain None.

If you want the indices of the masked values, you can do:

>>> numpy.nonzero(m.mask)

The documentation of numpy.nonzero() describes how its result must be interpreted.

EOL
Good point. After more investigation, I see that you're right. The values that are actually the problem appear to be ones I hadn't noticed before, that are actually "None".So, now I shall change the question to what it should have been: how do you go about finding the "None" values? A[A==None] just gives A[0,:] for some reason.
I added more information in my answer: I hope that you'll find what you need in it! :)
EOL
@dave-schultz: If your restatement in your comment above is a clearer statement of the question, it would be helpful if you edited your question to reflect this. Many more people will read your question than your comment here.
tom10
+2  A: 

To find the elements in a numpy array that are None, you can use numpy.equal. Here's an example:

import numpy as np
import MA

x = np.array([1, 2, None])

print np.equal(x, None)
# array([False, False,  True], dtype=bool)

# to get a masked array
print MA.array(x, mask=np.equal(x,None))
# [1 ,2 ,-- ,]
tom10