Skip to content

numpy ma masked_equal returns ValueError: setting an array element with a sequence. when fill_value is float and missing_value is np.ndarray: setting anything but 0-dim array to scalar not allowed anymore in 2.4.0 #30591

@valeriupredoi

Description

@valeriupredoi

Describe the issue:

Hi folks, not sure if a bug - at any rate, a rather peculiar case: given a masked array, that has a fill_value, numpy ma.masked_equal fails when passing an ndarray (single element) for missing_value, works fine if float; this is new behaviour for numpy 2.4.0. Cheers very much! 🍺

Reproduce the code example:

import numpy as np


# works fine
missing_value = 33.
data = np.ma.array(
    [[[-900., 33.], [33., -900], [33., 44.]]],
    mask=False,
    fill_value=-900.0,
    dtype=float
)
s = np.ma.masked_equal(data, missing_value)
print(s)

# fails only with numpy=2.4.0
missing_value = np.array([33.], dtype=float)
data = np.ma.array(
    [[[-900., 33.], [33., -900], [33., 44.]]],
    mask=False,
    fill_value=-900.0,
    dtype=float
)
s = np.ma.masked_equal(data, missing_value)
print(s)

Error message:

TypeError: only 0-dimensional arrays can be converted to Python scalars

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/valeriu/numpy_mre.py", line 23, in <module>
    s = np.ma.masked_equal(data, missing_value)
  File "/home/valeriu/miniconda3/envs/pyactive5/lib/python3.14/site-packages/numpy/ma/core.py", line 2161, in masked_equal
    output.fill_value = value
    ^^^^^^^^^^^^^^^^^
  File "/home/valeriu/miniconda3/envs/pyactive5/lib/python3.14/site-packages/numpy/ma/core.py", line 3850, in fill_value
    _fill_value[()] = target
    ~~~~~~~~~~~^^^^
ValueError: setting an array element with a sequence.

Python and NumPy Versions:

Python 3.14 and numpy 2.4.0

Runtime Environment:

No response

How does this issue affect you or how did you find it:

All fine, we fixed it so we use only floats for missing_value but others may be affected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    00 - Bug57 - Close?Issues which may be closable unless discussion continued

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions