12

I have some arrays that contain masked elements (from Numpy.MaskedArray), e.g.

data = [0,1,masked,3,masked,5,...]

Where the mask doesn't follow a regular pattern.

I want to iterate through the array and simply delete all elements that are masked to end up with:

data = [0,1,3,5,...]

I've tried a loop like:

for i in xrange(len(data)):
    if np.ma.is_masked(data[i]):
        data.pop(i)

But I get the error: local variable 'data' referenced before assignment

Do I have to create a new array and add the unmasked elements? Or is there a MaskedArray function that can automatically do this? I've had a look at the documentation but it's not obvious to me.

Thanks!

rh1990
  • 880
  • 7
  • 17
  • 32

2 Answers2

16

data.compressed() is the function you're looking for

Eric
  • 95,302
  • 53
  • 242
  • 374
  • This doesn't work, I get the `AttributeError: 'list' object has no attribute 'compress'` error – rh1990 Jul 13 '17 at 15:20
  • @RichardHall Based on this error, it looks like you may have typed the function name incorrectly. @Eric's solution says `compressed` and your error says `compress`. – emem_tee Jan 28 '21 at 15:41
  • 1
    @emem_tee, it also looks like `data` was a `list`, and not a numpy masked array (`numpy.ma.MaskedArray`). – Kaleb Coberly Jan 29 '21 at 01:21
13

With mask bitwise invertion ~:

data = data[~data.mask]
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
  • I get the error: `AttributeError: 'list' object has no attribute 'mask'` – rh1990 Jul 13 '17 at 15:21
  • @RichardHall, but you have wrote that you have masked numpy array – RomanPerekhrest Jul 13 '17 at 15:30
  • 1
    Actually I found it, I converted my masked elements to NaNs and didn't see, so I was getting these errors. Your solution worked when I removed that, thanks :) – rh1990 Jul 13 '17 at 15:34
  • 1
    Note that unlike my solution, this returns a masked array, not a normal array – Eric Jul 13 '17 at 17:04
  • should you return data[~data.mask].data? Otherwise it is still masked array with all performance overheads. – Vlad Apr 14 '20 at 19:48
  • 2
    @Vlad and Eric, can you describe how you determined that they were different array types? New users may not know how to inspect objects for extra attributes. `np.array_equal(data.compressed(), data[~data.mask])` resolved to `True`. – emem_tee Jan 28 '21 at 15:43