summaryrefslogtreecommitdiff
path: root/numpy/polynomial/_polybase.py
diff options
context:
space:
mode:
authorRoss Barnowski <rossbar@berkeley.edu>2020-05-07 14:04:24 -0700
committerRoss Barnowski <rossbar@berkeley.edu>2020-05-13 15:01:36 -0700
commit66365a57ff9b68a191fd232dc823cc2cf69d267f (patch)
tree2ad0e30d6cff55d893333432515dafeabee56c8f /numpy/polynomial/_polybase.py
parent9c83b13ce1b08aed8181d284566d002086393a89 (diff)
downloadnumpy-66365a57ff9b68a191fd232dc823cc2cf69d267f.tar.gz
Handle TypeError in _generate_str for coefs.
Add a fallback for TypeErrors that are raised when attempting to compare arbitrary elements (e.g. strings or Python complex) to 0 in _generate_str.
Diffstat (limited to 'numpy/polynomial/_polybase.py')
-rw-r--r--numpy/polynomial/_polybase.py12
1 files changed, 9 insertions, 3 deletions
diff --git a/numpy/polynomial/_polybase.py b/numpy/polynomial/_polybase.py
index 253aa74e8..7616b95c7 100644
--- a/numpy/polynomial/_polybase.py
+++ b/numpy/polynomial/_polybase.py
@@ -347,10 +347,16 @@ class ABCPolyBase(abc.ABC):
out += " "
power = str(i + 1)
# Polynomial coefficient
- if coef >= 0:
+ try:
+ if coef >= 0:
+ next_term = f"+ {coef}"
+ else:
+ next_term = f"- {-coef}"
+ # The coefficient array can be an object array with elements that
+ # will raise a TypeError with >= 0 (e.g. strings or Python
+ # complex). In this case, represent the coeficient as-is.
+ except TypeError:
next_term = f"+ {coef}"
- else:
- next_term = f"- {-coef}"
# Polynomial term
next_term += term_method(power, "x")
# Length of the current line with next term added