-
-
Notifications
You must be signed in to change notification settings - Fork 11.7k
Open
Labels
Milestone
Description
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.