summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBas van Beek <b.f.van.beek@vu.nl>2020-08-11 16:17:50 +0200
committerBas van Beek <b.f.van.beek@vu.nl>2020-08-11 16:17:50 +0200
commite62d5d15f3b8df247dcb133eaaceb71f5f60d8c0 (patch)
tree7fb2c806956659606f08df1ff19a34bda8c7ee2d
parent7127cdfb8553030ba317455c9f31fe4d7ed74e81 (diff)
downloadnumpy-e62d5d15f3b8df247dcb133eaaceb71f5f60d8c0.tar.gz
MAINT: Catching warnings is expensive; remove it
-rw-r--r--numpy/lib/function_base.py11
1 files changed, 6 insertions, 5 deletions
diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py
index 96e1c6de9..e9fe936e9 100644
--- a/numpy/lib/function_base.py
+++ b/numpy/lib/function_base.py
@@ -1644,12 +1644,13 @@ def trim_zeros(filt, trim='fb'):
def _trim_zeros_new(filt, trim='fb'):
"""Newer optimized implementation of ``trim_zeros()``."""
arr_any = np.asanyarray(filt)
- with warnings.catch_warnings():
- # not all dtypes support elementwise comparisons with `0` (e.g. str)
- warnings.simplefilter('error', FutureWarning)
- arr = arr_any != 0 if arr_any.dtype != bool else arr_any
+ arr = arr_any != 0 if arr_any.dtype != bool else arr_any
- if arr.ndim != 1:
+ if arr is False:
+ # not all dtypes support elementwise comparisons with `0` (e.g. str);
+ # they will return `False` instead
+ raise TypeError('elementwise comparison failed; unsupported data type')
+ elif arr.ndim != 1:
raise ValueError('trim_zeros requires an array of exactly one dimension')
elif not len(arr):
return filt