numpy/numpy · error · TypeError
threshold must be numeric
Error message
threshold must be numeric
What it means
Raised as TypeError by set_printoptions when threshold is not an instance of numbers.Number. threshold controls how many elements trigger summarization; this guard (added per gh-12351) rejects a common bad value such as a string that was historically suggested online.
Source
Thrown at numpy/_core/arrayprint.py:107
elif legacy == '1.21':
options['legacy'] = 121
elif legacy == '1.25':
options['legacy'] = 125
elif legacy == '2.1':
options['legacy'] = 201
elif legacy == '2.2':
options['legacy'] = 202
elif legacy is None:
pass # OK, do nothing.
else:
warnings.warn(
"legacy printing option can currently only be '1.13', '1.21', "
"'1.25', '2.1', '2.2' or `False`", stacklevel=3)
if threshold is not None:
# forbid the bad threshold arg suggested by stack overflow, gh-12351
if not isinstance(threshold, numbers.Number):
raise TypeError("threshold must be numeric")
if np.isnan(threshold):
raise ValueError("threshold must be non-NAN, try "
"sys.maxsize for untruncated representation")
if precision is not None:
# forbid the bad precision arg as suggested by issue #18254
try:
options['precision'] = operator.index(precision)
except TypeError as e:
raise TypeError('precision must be an integer') from e
return options
@set_module('numpy')
def set_printoptions(precision=None, threshold=None, edgeitems=None,
linewidth=None, suppress=None, nanstr=None,
infstr=None, formatter=None, sign=None, floatmode=None,View on GitHub (pinned to e117b3ca4e)
Solutions
- Pass an int (e.g. 1000) or sys.maxsize to disable summarization
- Coerce external input: threshold = int(threshold) before calling
- Ensure threshold is a numbers.Number (int, float that is not NaN)
Example fix
# before np.set_printoptions(threshold='all') # after import sys np.set_printoptions(threshold=sys.maxsize)
Defensive patterns
Strategy: type-guard
Validate before calling
import numbers
def safe_threshold(thr):
if not isinstance(thr, numbers.Number):
raise TypeError(f"threshold must be numeric, got {type(thr).__name__}")
return int(thr)
# np.set_printoptions(threshold=safe_threshold(user_thr)) Type guard
import numbers
def is_numeric_threshold(thr) -> bool:
return isinstance(thr, numbers.Number) and not isinstance(thr, bool) Try / catch
try:
np.set_printoptions(threshold=thr)
except TypeError as e:
if 'threshold must be numeric' in str(e):
import sys
np.set_printoptions(threshold=sys.maxsize)
else:
raise Prevention
- Coerce config-sourced thresholds to int at load time
- Use sys.maxsize for untruncated output, not a string
- Validate numeric options before applying
When it happens
Trigger: np.set_printoptions(threshold='all'); threshold=np.inf supplied as a string; passing a non-numeric object.
Common situations: Copying outdated Stack Overflow advice; deserializing threshold from JSON as a string; UI controls returning text.
Related errors
- threshold must be non-NAN, try sys.maxsize for untruncated r
- precision must be an integer
- floatmode option must be one of "fixed", "unique", "maxprec"
- sign option must be one of ' ', '+', or '-'
- unsupported order value: {order}
AI-assisted analysis of numpy/numpy@e117b3ca4e (2026-08-07).
Data as JSON: /api/errors/f5e7b74ca323c907.
Report an issue: GitHub.