{"record":{"id":"79592b29a443e920","repo":"pola-rs/polars","slug":"cannot-treat-numpy-array-of-type-arr-dtype-as-in","errorCode":null,"errorMessage":"cannot treat NumPy array of type {arr.dtype} as indices","messagePattern":"cannot treat NumPy array of type (.+?) as indices","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/_utils/getitem.py","lineNumber":425,"sourceCode":"    #     to absolute indexes.\n    if arr.ndim == 0:\n        arr = np.atleast_1d(arr)\n    if arr.ndim != 1:\n        msg = \"only 1D NumPy arrays can be treated as indices\"\n        raise TypeError(msg)\n\n    idx_type = get_index_type()\n\n    if len(arr) == 0:\n        return pl.Series(\"\", [], dtype=idx_type)\n\n    # Numpy array with signed or unsigned integers.\n    if arr.dtype.kind not in (\"i\", \"u\"):\n        if arr.dtype.kind == \"b\":\n            _raise_on_boolean_mask()\n        else:\n            msg = f\"cannot treat NumPy array of type {arr.dtype} as indices\"\n            raise TypeError(msg)\n\n    if idx_type == UInt32:\n        if arr.dtype in {np.int64, np.uint64} and arr.max() >= U32_MAX:\n            msg = \"index positions should be smaller than 2^32\"\n            raise ValueError(msg)\n        if arr.dtype == np.int64 and arr.min() < -U32_MAX:\n            msg = \"index positions should be greater than or equal to -2^32\"\n            raise ValueError(msg)\n\n    if arr.dtype.kind == \"i\" and arr.min() < 0:\n        if idx_type == UInt32:\n            if arr.dtype in (np.int8, np.int16):\n                arr = arr.astype(np.int32)\n        else:\n            if arr.dtype in (np.int8, np.int16, np.int32):\n                arr = arr.astype(np.int64)\n\n        # Update negative indexes to absolute indexes.","sourceCodeStart":407,"sourceCodeEnd":443,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/_utils/getitem.py#L407-L443","documentation":"`_convert_np_ndarray_to_indices` accepts only arrays whose dtype kind is integer ('i' or 'u'). Boolean arrays are routed to a dedicated boolean-mask error; any other dtype (float, string, object) raises this TypeError because non-integer arrays cannot be positional indices.","triggerScenarios":"`s[np.array([0.0, 1.0])]`, `s[np.array(['a','b'])]`, or an index array produced by float division instead of integer division.","commonSituations":"Getting float indices from `np.linspace`, `/` division, or `np.where` on floats; loading indices from JSON which yields floats; passing string columns as selectors by mistake.","solutions":["Cast to an integer dtype first: `s[arr.astype(np.uint32)]`.","Use integer division `//` instead of `/` when computing positions.","For label lookups use `.filter()` or a join rather than positional indexing."],"exampleFix":"// before\narr = np.array([0.0, 2.0, 4.0])\nsub = s[arr]  # TypeError: cannot treat NumPy array of type float64...\n// after\nsub = s[arr.astype(np.int64)]","handlingStrategy":"type-guard","validationCode":"if isinstance(idx, np.ndarray) and idx.dtype.kind not in ('i', 'u'):\n    if idx.dtype.kind == 'b':\n        raise ValueError(\"use .filter() for boolean masks\")\n    idx = idx.astype(np.int64)","typeGuard":"def is_int_ndarray(x) -> bool:\n    return isinstance(x, np.ndarray) and x.dtype.kind in ('i', 'u')","tryCatchPattern":"try:\n    sub = s[np_idx]\nexcept TypeError as e:\n    if \"as indices\" in str(e):\n        sub = s[np.asarray(np_idx).astype(np.int64)]\n    else:\n        raise","preventionTips":["Cast float index arrays to int before indexing.","Use `//` instead of `/` for position arithmetic.","Send boolean arrays to `.filter()`."],"tags":["polars","numpy","dtype","indexing"],"backgroundTag":"unsupported-dtype","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}