Skip to content

np.ma.asarray looses mask for str array #18551

@hdkire

Description

@hdkire

Mask is lost when np.ma.asarray is called with masked and non-masked arrays and the masked array is of str type.
This behavior is a change from 1.18.5 to 1.19+.
When the masked array is of int type the mask is preserved.

Reproducing code example:

import numpy as np
masked_str = np.ma.masked_array(['a', 'b'], mask=[True, False])
normal_int = np.arange(2)
np.ma.asarray([masked_str, normal_int])

=>

masked_array(
  data=[['a', 'b'],
        ['0', '1']],
  mask=False,
  fill_value='N/A',
  dtype='<U21')
import numpy as np
masked_int = np.ma.masked_array([1, 2], mask=[True, False])
normal_int = np.arange(2)
np.ma.asarray([masked_int, normal_int])

=>

masked_array(
  data=[[--, 2],
        [0, 1]],
  mask=[[ True, False],
        [False, False]],
  fill_value=999999)

NumPy/Python version information:

1.20.1 3.8.6 (default, Jan 27 2021, 15:42:20)
[GCC 10.2.0]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions