jax-ml/jax · error · ValueError

np.delete(arr, obj): got obj.dtype={obj_array.dtype}; must b

Error message

np.delete(arr, obj): got obj.dtype={obj_array.dtype}; must be integer or bool.

What it means

Raised by jnp.delete when obj (after asarray) has a dtype that is neither integer nor boolean, e.g. float or string. JAX only supports integer positions or boolean masks for deletion; float indices that NumPy might accept via deprecated casting are rejected.

Source

Thrown at jax/_src/numpy/lax_numpy.py:7728

    obj_array -= arange(len(obj_array), dtype=obj_array.dtype)
    i = arange(a.shape[axis] - obj_array.size, dtype=obj_array.dtype)
    i += (i[None, :] >= obj_array[:, None]).sum(0, dtype=i.dtype)
    return a[(slice(None),) * axis + (i,)]

  # Case 3b: non-unique indices: must be static.
  obj_array = core.concrete_or_error(np.asarray, obj, "'obj' array argument of jnp.delete()")
  if issubdtype(obj_array.dtype, np.integer):
    # TODO(jakevdp): in theory this could be done dynamically if obj has no duplicates,
    # but this would require the complement of lax.gather.
    mask = np.ones(a.shape[axis], dtype=bool)
    mask[obj_array] = False
  elif obj_array.dtype == bool:
    if obj_array.shape != (a.shape[axis],):
      raise ValueError("np.delete(arr, obj): for boolean indices, obj must be one-dimensional "
                       "with length matching specified axis.")
    mask = ~obj_array
  else:
    raise ValueError(f"np.delete(arr, obj): got obj.dtype={obj_array.dtype}; must be integer or bool.")
  return a[tuple(slice(None) for i in range(axis)) + (mask,)]


@export
def insert(arr: ArrayLike, obj: ArrayLike | slice, values: ArrayLike,
           axis: int | None = None) -> Array:
  """Insert entries into an array at specified indices.

  JAX implementation of :func:`numpy.insert`.

  Args:
    arr: array object into which values will be inserted.
    obj: slice or array of indices specifying insertion locations.
    values: array of values to be inserted.
    axis: specify the insertion axis in the case of multi-dimensional
      arrays. If unspecified, ``arr`` will be flattened.

  Returns:

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Cast obj to an integer type: jnp.delete(a, obj.astype(int)) or jnp.array(obj, dtype=int)
  2. Fix upstream index computation to use integer division // or np.round(...).astype(int)
  3. If obj is boolean, keep it boolean — do not let it decay to float

Example fix

// before
jnp.delete(a, jnp.array([1.0, 2.0]))
// after
jnp.delete(a, jnp.array([1.0, 2.0], dtype=int))
Defensive patterns

Strategy: type-guard

Validate before calling

obj = jnp.asarray(obj)
if obj.dtype not in (jnp.bool_) and not jnp.issubdtype(obj.dtype, jnp.integer):
    obj = obj.astype(jnp.int32)

Type guard

def is_valid_delete_obj(obj):
    o = jnp.asarray(obj)
    return jnp.issubdtype(o.dtype, jnp.integer) or o.dtype == jnp.bool_

Prevention

When it happens

Trigger: jnp.delete(a, jnp.array([0.5, 1.0])) or passing a Python list of floats; passing a traced/dynamic array with weak dtype that materializes as float_; passing string or complex obj.

Common situations: Computing indices with division or np.where output that yields floats (e.g. len(a)/2 instead of len(a)//2); passing np.arange floats; NumPy-to-JAX port where float indices were deprecated-but-working.

Related errors


AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27). Data as JSON: /api/errors/5df8a3907a768b0f. Report an issue: GitHub.