Skip to content

BUG: np.choose results in rule 'safe' cast error in 32-bit Python #21305

Open
@t-wy

Description

@t-wy

Describe the issue:

It is often usual to use a column of type dtype('int64') from an array (e.g. from csv).
np.choose works correctly in 64-bit Python for using dtype('int64') array in parameter a, while it returns the casting error in 32-bit Python, which leads to the inconsistency.

(It is weird for dtype('int64') to be necessary for array index though, so here just addresses the inconsistency.)

The below is just the minimal code for the problem, the use case is usually like

np.choose(x[x < 5], [1, 2, 3], mode="clip")

in which x is in dtype('int64')

A current consistent workaround for all indices within the magnitude of 2^31 would be something like

np.choose(np.array([2]).astype(np.int32), [1, 2, 3], mode="clip")
np.choose(x[x < 5].astype(np.int32), [1, 2, 3], mode="clip")

Reproduce the code example:

import numpy as np
np.choose(123456789012345, [1, 2, 3], mode="clip")
# Expected Result: 3, working in 64-bit Python

Error message:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<__array_function__ internals>", line 5, in choose
  File "C:\Users\_____\AppData\Local\Programs\Python\Python38-32\lib\site-packages\numpy\core\fromnumeric.py", line 429, in choose
    return _wrapfunc(a, 'choose', choices, out=out, mode=mode)
  File "C:\Users\_____\AppData\Local\Programs\Python\Python38-32\lib\site-packages\numpy\core\fromnumeric.py", line 54, in _wrapfunc
    return _wrapit(obj, method, *args, **kwds)
  File "C:\Users\_____\AppData\Local\Programs\Python\Python38-32\lib\site-packages\numpy\core\fromnumeric.py", line 43, in _wrapit
    result = getattr(asarray(obj), method)(*args, **kwds)
TypeError: Cannot cast scalar from dtype('int64') to dtype('int32') according to the rule 'safe'

NumPy/Python version information:

1.21.2 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (Intel)]
1.22.3 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (Intel)]

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