[Numpy-discussion] Properties of ma.masked singleton

Alexander Belopolsky alexander.belopolsky at gmail.com
Fri Jan 6 08:27:15 EST 2006


In the current numpy version ma.masked singleton has the following  
properties:

 >>> len(ma.masked)
0

which is a change from old MA, where
 >>> len(MA.masked)
1

Similarly:

 >>> ma.masked.shape
(0,)
 >>> MA.masked.shape
()

It changes shape when filled:

 >>> ma.masked.filled()
array([999999])

and so on.

The code contains numerous "if x is masked" statements to support all  
this logic.

I would like to suggest a somewhat radical change for your review.

There are two main uses of ma.masked:

1. To set mask:

 >>> x = ma.zeros(5)
 >>> x[2:4] = ma.masked
 >>> print x
[0 0 -- -- 0]


2. To check if an element is masked:

 >>> x[2] is ma.masked
True


The second property allows a cute looking idiom "x[i] is masked", but  
only works for single element access:

 >>> x[2:4] is ma.masked
False

It also leads to strange results:

 >>> x[2].shape
(0,)


My proposal is to eliminate the property #2 from ma and redefine  
masked as None. Single element getitem will return a rank zero  
MaskedArray. We can also add "is_masked" property, which will allow a  
convenient check in the form "x[i].is_masked".

The main benefit of this proposal is that x[i] will be duck typing  
compatible with numpy scalars.

-- sasha




More information about the NumPy-Discussion mailing list