Skip to content

BUG: choose does an unnecessary cast of the initial out values #22237

@seberg

Description

@seberg

Describe the issue:

Choose (I think) always overwrites all values in the output. It is thus unnecessary to cast a given out to the resulting dtype (if a cast is necesssary).

This can lead to spurious warnings as given in the below example

Reproduce the code example:

import numpy as np

a = np.arange(3)
b = np.ones((3, 3), dtype=np.int64)

out = np.array([np.nan, np.nan, np.nan])

np.choose(a, b, out=out)

Error message:

/home/sebastian/forks/numpy/build/testenv/lib/python3.9/site-packages/numpy/core/fromnumeric.py:57: RuntimeWarning: invalid value encountered in cast
  return bound(*args, **kwds)

NumPy/Python version information:

All versions up to 1.24-dev will do the unnecessary cast, but only the current dev gives the spurious warning.
(We have a typing test that could run into this randomly, although I am changing it in a different PR)

Context for the issue:

No response

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