Skip to content

BUG: recarray, masked_array and datetime break fill value calculation #29818

@schoelle

Description

@schoelle

Describe the issue:

I am unable to sort a masked_array of records, as the records contain timestamps, and this breaks the minimum_fill_value function.

This might either be due to the unusual wrapping of the value into an extra array that was introduced by afcc560 (see Example 1) or it might be that there is an inconsistency on array construction in when unpacking values in the np.array function (see Example 2) which is a property that afcc560 relies on.

Reproduce the code example:

import numpy as np
import datetime as dt
# Example 1
rec = np.array([(dt.date(2025,4,1),)], dtype=[('foo','<M8[D]')])
ma = np.ma.masked_array(rec)
np.sort(ma)
# Example 2
np.array((np.array("Hello"),), dtype=[('foo', '<U8')]) # This works 
np.array((np.array(dt.date(2025,4,1)),), dtype=[('foo', '<M8[D]')]) # This does not

Error message:

Traceback (most recent call last):
  File "..../numpy_bug.py", line 20, in <module>
    np.ma.minimum_fill_value(ma)
  File "/usr/lib/python3/dist-packages/numpy/ma/core.py", line 347, in minimum_fill_value
    return _extremum_fill_value(obj, min_filler, "minimum")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/numpy/ma/core.py", line 296, in _extremum_fill_value
    return _recursive_fill_value(dtype, _scalar_fill_value)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/numpy/ma/core.py", line 211, in _recursive_fill_value
    return np.array(vals, dtype=dtype)[()]  # decay to void scalar from 0d
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: Could not convert object to NumPy datetime

Python and NumPy Versions:

1.26.4
3.12.3 (main, Aug 14 2025, 17:47:21) [GCC 13.3.0]

Runtime Environment:

No response

Context for the issue:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions