🌐 AI搜索 & 代理 主页
Skip to content

BUG: np.take cast to out argument #25588

@lorentzenchr

Description

@lorentzenchr

Describe the issue:

np.take(a, indices, out=out) fails if out.dtype is not exactly a.dtype. The doc says about out:

It should be of the appropriate shape and dtype.

But what is appropriate?

Reproduce the code example:

import numpy as np
a = np.arange(3).astype(np.int32)
indices = np.arange(2)
out = np.zeros_like(indices, dtype=np.int64)
np.take(a, indices, out=out)

Error message:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
File ~/github/numpy/build-install/usr/lib/python3.11/site-packages/numpy/_core/fromnumeric.py:59, in _wrapfunc(obj, method, *args, **kwds)
     58 try:
---> 59     return bound(*args, **kwds)
     60 except TypeError:
     61     # A TypeError occurs if the object does have such a method in its
     62     # class, but its signature is not identical to that of NumPy's. This
   (...)
     66     # Call _wrapit from within the except clause to ensure a potential
     67     # exception has a traceback chain.

TypeError: Cannot cast array data from dtype('int64') to dtype('int32') according to the rule 'safe'

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
Cell In[4], line 1
----> 1 np.take(a, indices, out=out)

File ~/github/numpy/build-install/usr/lib/python3.11/site-packages/numpy/_core/fromnumeric.py:192, in take(a, indices, axis, out, mode)
     95 @array_function_dispatch(_take_dispatcher)
     96 def take(a, indices, axis=None, out=None, mode='raise'):
     97     """
     98     Take elements from an array along an axis.
     99 
   (...)
    190            [5, 7]])
    191     """
--> 192     return _wrapfunc(a, 'take', indices, axis=axis, out=out, mode=mode)

File ~/github/numpy/build-install/usr/lib/python3.11/site-packages/numpy/_core/fromnumeric.py:68, in _wrapfunc(obj, method, *args, **kwds)
     59     return bound(*args, **kwds)
     60 except TypeError:
     61     # A TypeError occurs if the object does have such a method in its
     62     # class, but its signature is not identical to that of NumPy's. This
   (...)
     66     # Call _wrapit from within the except clause to ensure a potential
     67     # exception has a traceback chain.
---> 68     return _wrapit(obj, method, *args, **kwds)

File ~/github/numpy/build-install/usr/lib/python3.11/site-packages/numpy/_core/fromnumeric.py:45, in _wrapit(obj, method, *args, **kwds)
     43 except AttributeError:
     44     wrap = None
---> 45 result = getattr(asarray(obj), method)(*args, **kwds)
     46 if wrap:
     47     if not isinstance(result, mu.ndarray):

TypeError: Cannot cast array data from dtype('int64') to dtype('int32') according to the rule 'safe'

Python and NumPy Versions:

'numpy_version': '2.0.0.dev0+git20240115.bbdd595'

Runtime Environment:

not important

Context for the issue:

out = np.take(a, indices) works, but passing the out arg explicitly saves intermediate memory and could be a tiny bit faster.

Metadata

Metadata

Assignees

No one assigned

    Labels

    00 - BugsprintableIssue fits the time-frame and setting of a sprint

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions