[Numpy-discussion] numpy.ma.compress

Pierre GM pgmdevlist@gmail....
Thu Jan 24 03:58:04 CST 2008

On Thursday 24 January 2008 04:02:52 Stefan van der Walt wrote:
> I'm not 100% sure about the new behaviour -- compress now removes
> masked elements, instead of ignoring them.  Whereas a person would
> have been able to do
> compress(x,condition).compressed()
> before, the mask information is now thrown away.

Mmh, OK, I see. The problem arises when the condition is masked. Filling w/ 
False will get rid of the masked values, filling with True won't work either 
(cf example below). An option is then to simply take the condition as a 

masked_array(data = [1 2],
      mask = [False False],
masked_array(data = [1 2 -- --],
      mask = [False False  True  True],
masked_array(data = [1 2 --],
      mask = [False False  True],
masked_array(data = [1 2 --],
      mask = [False False  True],

> The numpy docstring states that compress should be equivalent to
> a[condition], which is no longer the case.
Good point... In most cases, as long as an axis is not specified. Note that 
the equivalence a.compress(condition) and a[condition] is not strictly true 
even for regular ndarrays: take a look at the example on the scipy site 

>>>b = array([[10,20,30],[40,50,60]])
>>>b.compress(b.ravel() >= 22)
array([30, 40, 50, 60])
IndexError: index (2) out of range (0<=index<=1) in dimension 0

Anyhow, I just commited an update fixing our initial problem (viz, forcing 
condition to a regular ndarray).

More information about the Numpy-discussion mailing list