0

I want to create a 1d masked array from a list of one-element (0d) masked arrays using numpy.ma. This works as expected if the values are not masked, it does not in the other case:

import numpy.ma as ma
ma.MaskedArray([ma.masked_array(data=-99, mask=False), ma.masked_array(data=-99, mask=False)])
ma.MaskedArray([ma.masked_array(data=-99, mask=True), ma.masked_array(data=-99, mask=True)])

The second commands produces

numpy.ma.core.MaskError: Cannot convert masked element to a Python int.

Is there a reason for this behaviour? Do I need to manually extract the data and the masks?

sebschub
  • 213
  • 4
  • 20
  • 2
    Would `ma.concatenate` help? https://numpy.org/doc/stable/reference/generated/numpy.ma.concatenate.html – slothrop Jun 06 '23 at 14:24
  • 1
    @slothrop With `ma.concatenate` I get a `ValueError: zero-dimensional arrays cannot be concatenated`, however, this triggered a further search and with that, I found `ma.stack` which seems to work. So thanks a lot! – sebschub Jun 06 '23 at 14:54
  • sorry about that but glad you got there in the end! – slothrop Jun 06 '23 at 14:55
  • For interest, how is your requirement different to `ma.array([-99,-99], mask=[True,True])` – user19077881 Jun 06 '23 at 14:57
  • @user19077881 I got a list of masked arrays as a result of a list comprehension, which I wanted to convert to a masked array. So the data and mask part were separated into different objects. Yes, I guess I would have had to convert my problem to the structure you proposed if I had not found a solution. – sebschub Jun 06 '23 at 15:09
  • I suspect there's a bug in `MaskedArray`. The first, with `False` for both elements runs. It's the `True` mask that's giving problems. But as long as `stack` works,.. – hpaulj Jun 06 '23 at 16:56
  • The fact that they are 0d, scalar arrays also contributes to the problem. – hpaulj Jun 06 '23 at 19:20
  • @hpaulj Yes, looks strange indeed. I will check the numpy repo again and will file a bug report if needed. – sebschub Jun 07 '23 at 07:08

0 Answers0