summaryrefslogtreecommitdiff
path: root/numpy/lib/function_base.py
diff options
context:
space:
mode:
authorFilip Ter <filip.ter@gmail.com>2021-02-28 15:07:39 -0800
committerFilip Ter <filip.ter@gmail.com>2021-02-28 15:07:39 -0800
commitbf72d67a8b02fe06ba1efa634cff1c9f6f26a39c (patch)
tree8e8b3231f75346a0e4341bf9f85c786f8a7a9186 /numpy/lib/function_base.py
parentf9dd21e64584f6e821805daa450b8bc9f8ef3526 (diff)
downloadnumpy-bf72d67a8b02fe06ba1efa634cff1c9f6f26a39c.tar.gz
ENH: A more helpful error message, when types don't match type of default kwarg
Diffstat (limited to 'numpy/lib/function_base.py')
-rw-r--r--numpy/lib/function_base.py16
1 files changed, 14 insertions, 2 deletions
diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py
index c6db42ce4..ddc919e4f 100644
--- a/numpy/lib/function_base.py
+++ b/numpy/lib/function_base.py
@@ -671,11 +671,23 @@ 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:
+ raise TypeError('Choicelist elements do not have a common dtype: {}'
+ .format(e))
+ 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:
+ raise TypeError(
+ 'Choicelists and default do not have a common dtype: {}'
+ .format(e))
# Convert conditions to arrays and broadcast conditions and choices
# as the shape is needed for the result. Doing it separately optimizes