summaryrefslogtreecommitdiff
path: root/numpy/lib/function_base.py
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2021-03-26 11:58:00 -0600
committerGitHub <noreply@github.com>2021-03-26 11:58:00 -0600
commit5ebc656271c2232a6cb7dc96ad63e8eeed3783fd (patch)
tree95b406039210832a8f75324884aada2d05b467af /numpy/lib/function_base.py
parent6af7320a7b8883337cd0c8f4233ba79b9b0885e8 (diff)
parent0d5eefb1ebd05f3d4aeaf2572460afe9d027aa8f (diff)
downloadnumpy-5ebc656271c2232a6cb7dc96ad63e8eeed3783fd.tar.gz
Merge pull request #18515 from terfilip/select-dtype-err-msg-18430
MAINT: Improve error message when common type not found.
Diffstat (limited to 'numpy/lib/function_base.py')
-rw-r--r--numpy/lib/function_base.py15
1 files changed, 13 insertions, 2 deletions
diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py
index 285f90a6f..af5a6e45c 100644
--- a/numpy/lib/function_base.py
+++ b/numpy/lib/function_base.py
@@ -671,11 +671,22 @@ def select(condlist, choicelist, default=0):
raise ValueError("select with an empty condition list is not possible")
choicelist = [np.asarray(choice) for choice in choicelist]
- choicelist.append(np.asarray(default))
+
+ try:
+ intermediate_dtype = np.result_type(*choicelist)
+ except TypeError as e:
+ msg = f'Choicelist elements do not have a common dtype: {e}'
+ raise TypeError(msg) from None
+ default_array = np.asarray(default)
+ choicelist.append(default_array)
# need to get the result type before broadcasting for correct scalar
# behaviour
- dtype = np.result_type(*choicelist)
+ try:
+ dtype = np.result_type(intermediate_dtype, default_array)
+ except TypeError as e:
+ msg = f'Choicelists and default value do not have a common dtype: {e}'
+ raise TypeError(msg) from None
# Convert conditions to arrays and broadcast conditions and choices
# as the shape is needed for the result. Doing it separately optimizes