[Numpy-discussion] bug with with fill_values in masked arrays?

Pierre GM pgmdevlist at gmail.com
Tue Mar 25 11:12:00 EDT 2008


On Tuesday 25 March 2008 10:33:58 Chris Withers wrote:
> Pierre GM wrote:
> > Well, yeah, my bad, that depends on whether you use masked_invalid or
> > fix_invalid or just build a basic masked array.
>
> Yeah, well, if there were any docs I'd have a *clue* what you were
> talking about ;-)

My bad, I neglected an overall doc for the functions and their docstring. But 
you know what ? As you're now at an intermediary level, you'll be able to 
help: just write down the problems you encountered, and the solutions you 
came up with, so that we could use your experience as the backbone for a 
proper MaskedArray documentation

> > Oh, and NaNs will be transformed to 0 if you use ints...
>
> "use ints" in what context?

Try that:
>>>x = numpy.ma.array([0,1,2,3,])
>>>x[-1] = numpy.nan
>>>print x
>>>[0 1 2 0]
See? No NaNs with an int array.


> > Nope, the idea is really is to make things as efficient as possible.
>
> For you, maybe. And for me, yes, except I wanted the NaNs to stick
> around...

Well, no problem, they should stick around. Note that if a NaN/Inf should 
normally show up as the result of some operation (divide by zero for 
example), it'll probably won't:
>>>x = numpy.ma.array([0,1,2,numpy.nan],dtype=float)
>>>print 1./x
>>>[-- 1.0 0.5 nan]
>>>print (1./x)._data 
>>>[ 1.   1.   0.5  NaN]
>>>print 1./x._data
>>>[ Inf  1.   0.5  NaN]


> I'd argue that the masked singleton having a different fill value to the
> ma it comes from is a bug.

"It's not a bug, it's a feature"TM

> > And once again, it's not. numpy.ma.masked is a special value, like
> > numpy.nan or numpy.inf
>
> ...which is silly, since that forces it to have a fixed fill value,
> which it should not.

The fill_value for the mask singleton is meaningless, correct. However, having 
numpy.ma.masked as a constant is really helpful to test whether a particular 
value is masked, or to mask a particular value:
>>>x = numpy.ma.array([0,1,2,3])
>>>x[-1] = masked
>>>x[-1] is masked
>>>True



More information about the NumPy-Discussion mailing list