| Version 7 (modified by sasha, 7 years ago) |
|---|
For a high-level overview of nan and masked array support, see also SciPy FAQ.
MaskedArray is a numpy ndarray look-alike that allows one to keep track of missing values.
MaskedArray is implemented in the [source:trunk/numpy/core/ma.py numpy.core.ma] module. The numpy ma module is originally written for Numeric by Paul Dubois and adapted for numpy by Travis Oliphant and (mainly) Paul Dubois.
Migration Guide
describe migration from Numeric.MA to numpy.ma here
Remaining Issues
Some of the same issues that were resolved in numpy need to be revisited for ma. (See Numeric3.0 Design Document)
- What does single element indexing return? Scalars or rank-0 arrays?
An additional complication is that that single element may be masked.
- What should a single element indexing return for an unmasked element?
- What should a single element indexing return for a masked element?
As of changeset:1882, the answer is ma.masked. The singleton ma.masked is defined in [source:tags/0.9.2/numpy/core/ma.py ma.py] as follows:This changed from MA, where masked was defined as a rank-0 array. This definition leads to some surprizing properties:masked = MaskedArray([0], int, mask=[1])[0:0] masked = masked[0:0]
>>> from numpy.core.ma import * >>> x = array([1,2,3.0]) >>> x[1].shape (0,)
At the same time>>> x[0].shape ()
This can easily be fixed by changing the definition of "masked" back to rank-0 array. (Done in changeset:1888)>>> x[1].dtype <type 'int64_arrtype'>
At the same time>>> x[0].dtype <type 'float64_arrtype'>
Unlike the first problem, this one cannot be easily fixed without giving up the ability to check for mising values using>>> x[1] is masked True >>> x[0] is masked False
It is tempting to eliminate the special case and just use x[i].mask.all() and x[i].mask.any(), the constructs that have clear meaning for any number of elements. The downside of changing the return value of x[i] for masked elements is that "x[i] is masked" will silently break in a dangerous way - it will always be false.
- What should a single element indexing return for an unmasked element?
It may be safer to also change the name "masked" to say "missing" and educate users that x[i] is masked should be changed to x[i].mask.any(), x[i].mask.all() or even just x[i].mask as appropriate and x[i] = masked should be changed to x[i] = missing.
- Can arrays be used as truth values directly?
Attachments
-
ma_examples.py
(2.3 KB) - added by Pierre GM
7 years ago.
Ideas of implementation of std & median for masked arrays
-
ma-20060321.patch
(3.2 KB) - added by pgmdevlist@…
7 years ago.
Suggestions for a 'ma' patch
-
ma-200603280900.patch
(3.1 KB) - added by Pierre GM
7 years ago.
new patch for MA
-
testnewma.py
(5.7 KB) - added by Pierre GM
7 years ago.
New test suite for MA patch
