{"record":{"id":"c3087c39062afee0","repo":"jax-ml/jax","slug":"np-delete-arr-obj-for-boolean-indices-obj-must","errorCode":null,"errorMessage":"np.delete(arr, obj): for boolean indices, obj must be one-dimensional with length matching specified axis.","messagePattern":"np\\.delete\\(arr, obj\\): for boolean indices, obj must be one-dimensional with length matching specified axis\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/lax_numpy.py","lineNumber":7724,"sourceCode":"        0,\n        a.shape[axis],\n    )\n    obj_array = sort(obj_array)\n    obj_array -= arange(len(obj_array), dtype=obj_array.dtype)\n    i = arange(a.shape[axis] - obj_array.size, dtype=obj_array.dtype)\n    i += (i[None, :] >= obj_array[:, None]).sum(0, dtype=i.dtype)\n    return a[(slice(None),) * axis + (i,)]\n\n  # Case 3b: non-unique indices: must be static.\n  obj_array = core.concrete_or_error(np.asarray, obj, \"'obj' array argument of jnp.delete()\")\n  if issubdtype(obj_array.dtype, np.integer):\n    # TODO(jakevdp): in theory this could be done dynamically if obj has no duplicates,\n    # but this would require the complement of lax.gather.\n    mask = np.ones(a.shape[axis], dtype=bool)\n    mask[obj_array] = False\n  elif obj_array.dtype == bool:\n    if obj_array.shape != (a.shape[axis],):\n      raise ValueError(\"np.delete(arr, obj): for boolean indices, obj must be one-dimensional \"\n                       \"with length matching specified axis.\")\n    mask = ~obj_array\n  else:\n    raise ValueError(f\"np.delete(arr, obj): got obj.dtype={obj_array.dtype}; must be integer or bool.\")\n  return a[tuple(slice(None) for i in range(axis)) + (mask,)]\n\n\n@export\ndef insert(arr: ArrayLike, obj: ArrayLike | slice, values: ArrayLike,\n           axis: int | None = None) -> Array:\n  \"\"\"Insert entries into an array at specified indices.\n\n  JAX implementation of :func:`numpy.insert`.\n\n  Args:\n    arr: array object into which values will be inserted.\n    obj: slice or array of indices specifying insertion locations.\n    values: array of values to be inserted.","sourceCodeStart":7706,"sourceCodeEnd":7742,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/lax_numpy.py#L7706-L7742","documentation":"Raised by jnp.delete when the boolean mask obj does not have exactly shape (a.shape[axis],). JAX implements delete by building a boolean keep-mask over the given axis, so a boolean obj must be 1-D and match the axis length exactly, unlike integer indices which can be arbitrary.","triggerScenarios":"Calling jnp.delete(a, mask, axis=k) where mask is boolean with shape != (a.shape[k],), e.g. a 2-D boolean array, a mask sized for a different axis, or the default axis=0 while the mask was built for another axis.","commonSituations":"Passing a column mask (shape (n,1)) instead of a flat mask; forgetting to specify axis when deleting rows vs columns; porting NumPy code where a differently-sized boolean was silently tolerated via integer conversion.","solutions":["Reshape/flatten the mask to 1-D with length equal to a.shape[axis] (e.g. mask.ravel() or mask[:, 0])","Pass the correct axis argument matching the mask's length","Convert the mask to integer indices: jnp.delete(a, jnp.where(mask)[0], axis=k)"],"exampleFix":"// before\njnp.delete(a, mask, axis=1)  # mask has shape (a.shape[0], 1)\n// after\njnp.delete(a, mask.ravel(), axis=0)  # or mask[:, 0] if mask is (n,1) targeting columns\n","handlingStrategy":"validation","validationCode":"mask = jnp.asarray(mask)\nassert mask.ndim == 1 and mask.shape[0] == a.shape[axis], 'mask must match axis length'","typeGuard":"def is_valid_delete_mask(a, mask, axis=0):\n    m = jnp.asarray(mask)\n    return m.dtype == jnp.bool_ and m.ndim == 1 and m.shape[0] == a.shape[axis]","tryCatchPattern":null,"preventionTips":["Build masks with shape (a.shape[axis],) via comparisons on that axis","Always pass axis explicitly when the mask targets a non-default axis","Flatten (n,1) masks with .ravel() before use"],"tags":["jax","numpy-compat","boolean-mask","shape-mismatch"],"backgroundTag":"boolean-mask-shape-mismatch","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}