{"record":{"id":"8db82dd7b396e25d","repo":"jax-ml/jax","slug":"jnp-insert-index-array-must-be-integer-typed-g","errorCode":null,"errorMessage":"jnp.insert(): index array must be integer typed; got {obj}","messagePattern":"jnp\\.insert\\(\\): index array must be integer typed; got (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/lax_numpy.py","lineNumber":7802,"sourceCode":"  if axis is None:\n    a = ravel(a)\n    axis = 0\n  axis = core.concrete_or_error(None, axis, \"axis argument of jnp.insert()\")\n  axis = _canonicalize_axis(axis, a.ndim)\n  if isinstance(obj, slice):\n    indices = arange(*obj.indices(a.shape[axis]))\n  else:\n    indices = asarray(obj)\n\n  if indices.ndim > 1:\n    raise ValueError(\"jnp.insert(): obj must be a slice, a one-dimensional \"\n                     f\"array, or a scalar; got {obj}\")\n  if not np.issubdtype(indices.dtype, np.integer):\n    if indices.size == 0 and not isinstance(obj, Array):\n      indices = indices.astype(int)\n    else:\n      # Note: np.insert allows boolean inputs but the behavior is deprecated.\n      raise ValueError(\"jnp.insert(): index array must be \"\n                       f\"integer typed; got {obj}\")\n  values_arr = array(values_arr, ndmin=a.ndim, dtype=a.dtype, copy=False)\n\n  if indices.size == 1:\n    index = ravel(indices)[0]\n    if indices.ndim == 0:\n      values_arr = moveaxis(values_arr, 0, axis)\n    indices = array_creation.full(values_arr.shape[axis], index)\n  n_input = a.shape[axis]\n  n_insert = broadcast_shapes(indices.shape, (values_arr.shape[axis],))[0]\n  out_shape = list(a.shape)\n  out_shape[axis] += n_insert\n  out = array_creation.zeros_like(a, shape=tuple(out_shape))\n\n  indices = where(indices < 0, indices + n_input, indices)\n  indices = clip(indices, 0, n_input)\n\n  values_ind = indices.at[argsort(indices)].add(arange(n_insert, dtype=indices.dtype))","sourceCodeStart":7784,"sourceCodeEnd":7820,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/lax_numpy.py#L7784-L7820","documentation":"Raised by jnp.insert when the insertion positions array has a non-integer dtype (e.g. float) and is non-empty (or is a JAX Array). NumPy deprecates boolean/float obj for insert, so JAX rejects non-integer index arrays outright, only casting empty non-Array inputs to int.","triggerScenarios":"jnp.insert(a, jnp.array([0.5]), values); insertion positions computed as floats (division instead of integer division); boolean obj arrays, which NumPy allowed but deprecated.","commonSituations":"Positions like n//2 accidentally written n/2; positions from np.linspace without astype(int); porting old NumPy code using boolean masks with insert.","solutions":["Cast positions to int: jnp.insert(a, obj.astype(int), values)","Fix position computation to use integer arithmetic (//, operator.index)","Convert boolean masks to positions with jnp.where(mask)[0] before inserting"],"exampleFix":"// before\njnp.insert(a, len(a)/2, 99)  # float index\n// after\njnp.insert(a, len(a)//2, 99)  # or int(len(a)/2)\n","handlingStrategy":"type-guard","validationCode":"obj = jnp.asarray(obj)\nif not jnp.issubdtype(obj.dtype, jnp.integer) and obj.size:\n    obj = obj.astype(jnp.int32)","typeGuard":"def is_integer_insert_positions(obj):\n    o = jnp.asarray(obj)\n    return o.size == 0 or jnp.issubdtype(o.dtype, jnp.integer)","tryCatchPattern":null,"preventionTips":["Compute positions with integer arithmetic","Convert boolean masks to indices via jnp.where(mask)[0]","Cast linspace/arange outputs to int before insert"],"tags":["jax","insert","dtype","indexing"],"backgroundTag":"float-array-index-error","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}