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

Skip to content

where with out-of-bounds Python integers in Numpy 2 #27006

@ndgrigorian

Description

@ndgrigorian

With Numpy 2 I've observed that where seems to behave in a (now strange) way with out-of-bounds Python integers compared to other functions.

The first argument of where does not participate in type promotion, so if using a Python integer as x1 or x2 and an array as the other argument, the output should be an array with the data type of the Numpy array argument.

However, unlike every ufunc with binary type promotion, where permits an out-of-bounds Python integer.

For example,

In [24]: np.where(np.asarray([True, False], dtype="?"), -1, np.asarray(0, dtype="u1"))
Out[24]: array([255,   0], dtype=uint8)

where for any other function

In [28]: np.add(np.asarray(0, dtype="u1"), -1)
---------------------------------------------------------------------------
OverflowError                             Traceback (most recent call last)
Cell In[28], line 1
----> 1 np.add(np.asarray(0, dtype="u1"), -1)

OverflowError: Python integer -1 out of bounds for uint8

In [29]: np.clip(np.asarray(0, dtype=np.uint8), -1, np.asarray(0, dtype=np.uint8))
---------------------------------------------------------------------------
OverflowError                             Traceback (most recent call last)
...
OverflowError: Python integer -1 out of bounds for uint8

Given that not even assignment now permits out-of-bounds Python integers, it seems like an oversight.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions