BUG: `take` casting logic with an `out=` argument
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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