numpy / numpy/numpy

BUG: np.select does not return values in choicelist when default used is empty list

Open
#21,092 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

00 - Bug 04 - Documentation
Dominant language
Python
Stars
32.8k
Forks
12.8k
Avg merge
1d 7h
Merged PRs (30d)
197

Description

Describe the issue:

I want to have empty list as default value of np.select.

When the default value is selected to be an empty list or tuple, choicelist will be overwritten from [array(1), array(2), array(3), array([], dtype=float64)] to [array([], dtype=int64), array([], dtype=int64), array([], dtype=int64), array([], dtype=float64)]
after choicelist = np.broadcast_arrays(*choicelist) in function_base.py.

The result is that I get array([], dtype=float64) returned when i expect 3 to be returned (because the conditions are False,False,True corresponding to 1,2,3).

Reproduce the code example:
import numpy as np

conds = [False, 
         False, 
         True]

print(np.select(conds, [1,2,3], default=[]))


### Error message:

_No response_

### NumPy/Python version information:

1.21.4 3.8.12 (default, Dec  8 2021, 11:22:00) 
[Clang 12.0.5 (clang-1205.0.22.11)]

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

Start at np.select and the choicelist broadcasting in function_base.py, then run the supplied reproduction with an empty-list default. Trace why broadcasting changes the scalar choices and add or update coverage for this case. Done means the True condition returns 3 while the empty-list default remains supported.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.