jax-ml/jax · error · ValueError
method can only be 'linear', 'lower', 'higher', 'midpoint',
Error message
method can only be 'linear', 'lower', 'higher', 'midpoint', 'nearest' or 'inverted_cdf'
What it means
The method argument of jnp.quantile / jnp.nanquantile must be one of the six supported interpolation/selection methods: linear, lower, higher, midpoint, nearest, inverted_cdf. Anything else — including valid NumPy method names JAX has not implemented — raises ValueError.
Source
Thrown at jax/_src/numpy/reductions.py:2518
>>> x = jnp.array([1, 2, jnp.nan, 4, 5])
>>> weights = jnp.array([1, 1, 1, 2, 1])
>>> jnp.nanquantile(x, 0.5, weights=weights, method='inverted_cdf')
Array(4.0, dtype=float32)
"""
a, q = ensure_arraylike("nanquantile", a, q)
if weights is not None:
weights = ensure_arraylike("nanquantile", weights)
if overwrite_input or out is not None:
msg = ("jax.numpy.nanquantile does not support overwrite_input=True or "
"out != None")
raise ValueError(msg)
return _quantile(a, q, axis, method, keepdims, True, weights)
def _quantile(a: Array, q: Array, axis: int | tuple[int, ...] | None,
method: str, keepdims: bool, squash_nans: bool, weights: Array | None = None) -> Array:
if method not in ["linear", "lower", "higher", "midpoint", "nearest", "inverted_cdf"]:
raise ValueError("method can only be 'linear', 'lower', 'higher', 'midpoint', 'nearest' or 'inverted_cdf'")
if weights is not None:
if dtypes.issubdtype(weights.dtype, np.complexfloating):
raise ValueError("Weights cannot be complex types.")
if method != "inverted_cdf":
raise NotImplementedError(f"{method} doesn't support weights. Only method 'inverted_cdf' supports weights.")
a, weights = promote_dtypes_inexact(a, weights)
if weights.shape != a.shape:
if axis is None:
raise ValueError("Weights shape must match 'a' shape when axis is None.")
ax_tuple = canonicalize_axis_tuple(axis, a.ndim)
if weights.shape != tuple(a.shape[ax] for ax in ax_tuple):
raise ValueError(f"Weights shape {weights.shape} must match reduction axes "
f"{tuple(a.shape[ax] for ax in ax_tuple)}")
weights = lax.broadcast_in_dim(weights, a.shape, broadcast_dimensions=ax_tuple)
else:
a, = promote_dtypes_inexact(a)
keepdim = []
if dtypes.issubdtype(a.dtype, np.complexfloating):View on GitHub (pinned to 1e1c6a8fc0)
Solutions
- Switch to one of: 'linear', 'lower', 'higher', 'midpoint', 'nearest', 'inverted_cdf'
- For NumPy-only methods like 'median_unbiased', compute with numpy on the host or implement manually
- Add a whitelist check in config-driven code to fail fast with a clear message
Example fix
// before jnp.quantile(a, q, method='median_unbiased') // after jnp.quantile(a, q, method='nearest')
Defensive patterns
Strategy: validation
Validate before calling
JAX_QUANTILE_METHODS = {'linear','lower','higher','midpoint','nearest','inverted_cdf'}
assert method in JAX_QUANTILE_METHODS, f'method must be one of {JAX_QUANTILE_METHODS}'
jnp.quantile(a, q, method=method) Type guard
def is_supported_quantile_method(m: str) -> bool:
return m in {'linear','lower','higher','midpoint','nearest','inverted_cdf'} Prevention
- Keep a project whitelist of JAX quantile methods
- Don't assume NumPy's full method list transfers to JAX
- Pin down method strings in configs to avoid free-form input
When it happens
Trigger: Calling jnp.quantile(a, q, method='averaged_inverted_cdf'), method='hazen', 'weibull', 'median_unbiased', 'normal_unbiased', or old interpolation='linear' strings passed as method.
Common situations: Using NumPy ≥1.22 method names (many extra methods exist in NumPy but not JAX); copy-pasting scipy.stats.mstats method names; passing the deprecated interpolation kwarg value as method.
Related errors
- jax.numpy.quantile does not support overwrite_input=True or
- Weights cannot be complex types.
- {method} doesn't support weights. Only method 'inverted_cdf'
- Weights shape must match 'a' shape when axis is None.
- Weights shape {weights.shape} must match reduction axes {tup
AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27).
Data as JSON: /api/errors/ca3cc1c705e3d9d8.
Report an issue: GitHub.