{"record":{"id":"c4bd72526a5b7d72","repo":"jax-ml/jax","slug":"expected-an-input-array-of-integer-or-boolean-data","errorCode":null,"errorMessage":"Expected an input array of integer or boolean data type","messagePattern":"Expected an input array of integer or boolean data type","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/lax_numpy.py","lineNumber":8695,"sourceCode":"\n    For a multi-dimensional input, bits may be packed along a specified axis:\n\n    >>> a = jnp.array([[1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0],\n    ...                [0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1]])\n    >>> vals = jnp.packbits(a, axis=1)\n    >>> vals\n    Array([[212, 150],\n           [ 69, 207]], dtype=uint8)\n\n    The inverse of ``packbits`` is provided by :func:`~jax.numpy.unpackbits`:\n\n    >>> jnp.unpackbits(vals, axis=1)\n    Array([[1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0],\n           [0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1]], dtype=uint8)\n  \"\"\"\n  arr = util.ensure_arraylike(\"packbits\", a)\n  if not (issubdtype(arr.dtype, np.integer) or issubdtype(arr.dtype, np.bool_)):\n    raise TypeError('Expected an input array of integer or boolean data type')\n  if bitorder not in ['little', 'big']:\n    raise ValueError(\"'order' must be either 'little' or 'big'\")\n  arr = lax.ne(arr, lax._const(arr, 0)).astype('uint8')\n  bits = arange(8, dtype='uint8')\n  if bitorder == 'big':\n    bits = bits[::-1]\n  if axis is None:\n    arr = ravel(arr)\n    axis = 0\n  arr = swapaxes(arr, axis, -1)\n\n  remainder = arr.shape[-1] % 8\n  if remainder:\n    arr = lax.pad(arr, np.uint8(0),\n                  (arr.ndim - 1) * [(0, 0, 0)] + [(0, 8 - remainder, 0)])\n\n  arr = arr.reshape(arr.shape[:-1] + (arr.shape[-1] // 8, 8))\n  bits = expand_dims(bits, tuple(range(arr.ndim - 1)))","sourceCodeStart":8677,"sourceCodeEnd":8713,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/lax_numpy.py#L8677-L8713","documentation":"Raised by jnp.packbits when the input's dtype is neither integer nor boolean — packbits packs each element into one bit, which is only meaningful for integral/boolean data.","triggerScenarios":"jnp.packbits(float_array); input from a computation that yields float32/float64; passing strings or complex arrays.","commonSituations":"Packing boolean results of comparisons that were later converted to float; feeding normalized/standardized numeric features into packbits by mistake; NumPy code where uint8 was implicit.","solutions":["Convert to bool first: jnp.packbits(a.astype(bool)) — nonzero/true packs as 1","If floats encode bit values, threshold explicitly: (a > 0).astype(bool)","Verify the pipeline stage: packbits is for bit-packing, not general compression"],"exampleFix":"// before\njnp.packbits(scores)  # float32\n// after\njnp.packbits(scores > 0.5)\n","handlingStrategy":"type-guard","validationCode":"arr = jnp.asarray(a)\nif not (jnp.issubdtype(arr.dtype, jnp.integer) or arr.dtype == jnp.bool_):\n    arr = arr.astype(bool)","typeGuard":"def packbits_compatible(a):\n    d = jnp.asarray(a).dtype\n    return jnp.issubdtype(d, jnp.integer) or d == jnp.bool_","tryCatchPattern":null,"preventionTips":["Binarize floats with a comparison before packbits","astype(bool) is the cheapest safe conversion","Reserve packbits for genuine bit data"],"tags":["jax","packbits","dtype-validation"],"backgroundTag":"dtype-validation-failed","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}