numpy / numpy/numpy

BUG: `take` casting logic with an `out=` argument

Open
#16,319 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

00 - Bug component: numpy._core
Dominant language
Python
Stars
32.8k
Forks
12.8k
Avg merge
1d 7h
Merged PRs (30d)
197

Description

From the related PR: #4246

When it receives an out argument, np.take converts the out array to

the dtype of the array being taken from. This results in wrong TypeErrors
being raised when out has a larger dtype than the array (and not being
raised when it has a smaller dtype, even though they should):

a = np.arange(3, dtype=np.int16) + 128
b = np.empty((3,), dtype=np.int8)
a.take([0, 1, 2], out=b) # wrong, should raise an error
array([-128, -127, -126], dtype=int8)
b = np.empty((3,), dtype=np.int32)
a.take([0, 1, 2], out=b) # perfectly OK, but raises an error
Traceback (most recent call last):
File "", line 1, in
TypeError: Cannot cast array data from dtype('int32') to dtype('int16') according to the rule 'safe'
This PR makes an explicit casting check, then either sets NPY_ARRAY_FORCECAST
or explicitly raises the error.

The tests added check the logic for conversion between all pairs of ints and
floats only.

Please see the related PR for the existing work on the issue.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Begin at np.take and review related PR #4246 and its added casting tests. Confirm the expected out= behavior for integer and floating-point dtype pairs; done means incorrect casts raise TypeError and valid casts succeed.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.