Thanks to visit codestin.com
Credit goes to github.com

Skip to content

BUG: numpy.ma.min fails for uint dtypes #27580

Open
@jbkoch7

Description

@jbkoch7

Describe the issue:

The numpy.ma.min operation fails for cases where the input array has a dtype = uint and the resultant array and mask has ndim > 0. There could be other operations where this same case fails, e.g., numpy.ma.max.

Reproduce the code example:

import numpy as np
import numpy.ma as ma

_dtype = np.uint8
fill_value = _dtype( 0 )
a = np.array( [ [ 1, 127, 2 ],
                [ 2, 127, 1 ] ], dtype = _dtype )
a = ma.masked_array( a, a == 127 )
a.fill_value = fill_value
amin = a.min( axis = 0 )

Error message:

╭───────────────────────── Traceback (most recent call last) ──────────────────────────╮
│ test.py:10 in <module>                                                               │
│                                                                                      │
│    7 │   │   │   │   [ 2, 127, 1 ] ], dtype = _dtype )                               │
│    8 a = ma.masked_array( a, a == 127 )                                              │
│    9 a.fill_value = fill_value                                                       │
│ ❱ 10 amin = a.min( axis = 0 )                                                        │
│   11                                                                                 │
│                                                                                      │
│ .venv/lib/python3.12/site-packages/numpy/ma/core.py:5985                             │
│ in min                                                                               │
│                                                                                      │
│   5982 │   │   │   │   result.__setmask__(newmask)                                   │
│   5983 │   │   │   │   # Get rid of Infs                                             │
│   5984 │   │   │   │   if newmask.ndim:                                              │
│ ❱ 5985 │   │   │   │   │   np.copyto(result, result.fill_value, where=newmask)       │
│   5986 │   │   │   elif newmask:                                                     │
│   5987 │   │   │   │   result = masked                                               │
│   5988 │   │   │   return result                                                     │
╰──────────────────────────────────────────────────────────────────────────────────────╯
TypeError: Cannot cast scalar from dtype('int64') to dtype('uint8') according to the
rule 'same_kind'

Python and NumPy Versions:

2.1.2
3.12.5 (main, Oct 14 2024, 12:30:20) [GCC 13.2.0]

Runtime Environment:

No response

Context for the issue:

No response

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