pola-rs/polars · error · TypeError
cannot treat NumPy array of type {arr.dtype} as indices
Error message
cannot treat NumPy array of type {arr.dtype} as indices What it means
In _convert_np_ndarray_to_indices (getitem.py:425), numpy index arrays must have integer dtype (kind 'i' or 'u'); boolean arrays route to the boolean-mask error, and every other dtype — float, str, datetime64, object — raises this TypeError naming arr.dtype.
Source
Thrown at py-polars/src/polars/_utils/getitem.py:425
# to absolute indexes.
if arr.ndim == 0:
arr = np.atleast_1d(arr)
if arr.ndim != 1:
msg = "only 1D NumPy arrays can be treated as indices"
raise TypeError(msg)
idx_type = get_index_type()
if len(arr) == 0:
return pl.Series("", [], dtype=idx_type)
# Numpy array with signed or unsigned integers.
if arr.dtype.kind not in ("i", "u"):
if arr.dtype.kind == "b":
_raise_on_boolean_mask()
else:
msg = f"cannot treat NumPy array of type {arr.dtype} as indices"
raise TypeError(msg)
if idx_type == UInt32:
if arr.dtype in {np.int64, np.uint64} and arr.max() >= U32_MAX:
msg = "index positions should be smaller than 2^32"
raise ValueError(msg)
if arr.dtype == np.int64 and arr.min() < -U32_MAX:
msg = "index positions should be greater than or equal to -2^32"
raise ValueError(msg)
if arr.dtype.kind == "i" and arr.min() < 0:
if idx_type == UInt32:
if arr.dtype in (np.int8, np.int16):
arr = arr.astype(np.int32)
else:
if arr.dtype in (np.int8, np.int16, np.int32):
arr = arr.astype(np.int64)
# Update negative indexes to absolute indexes.View on GitHub (pinned to df599052da)
Solutions
- Cast to integer dtype: df[arr.astype(np.int64)] (floor/round first if fractional)
- For value-based matching use df.filter(pl.col('c').is_in(arr.tolist()))
- Ensure index-producing numpy ops stay integral (e.g. use np.flatnonzero instead of manual float math)
Example fix
# before df[np.array([0.5, 1.5])] # after df[np.array([0.5, 1.5]).round().astype(np.int64)]
Defensive patterns
Strategy: type-guard
Validate before calling
import numpy as np
if arr.dtype.kind not in ("i", "u"):
if arr.dtype.kind == "b":
arr = np.flatnonzero(arr) # or use df.filter(mask)
else:
arr = arr.astype(np.int64)
df[arr] Type guard
import numpy as np
def is_integer_index_array(arr) -> bool:
return isinstance(arr, np.ndarray) and arr.ndim == 1 and arr.dtype.kind in ("i", "u") Prevention
- Cast float index arrays with .astype(np.int64) after rounding
- Watch for object dtype when converting from pandas; use .astype(np.int64) explicitly
- Use df.filter(...is_in(...)) for value matching rather than string arrays
When it happens
Trigger: df[np.array([0.5, 1.5])] (floats from division or np.mean); df[np.array(['a', 'b'])]; df[np.array(['2024-01-01'], dtype='datetime64[ns]')]; df[np.array([1, 2], dtype=object)] from pandas .to_numpy() on mixed columns.
Common situations: numpy operations that upcast indices to float (e.g. np.isnan-affected arrays, division); converting pandas object columns to positions; string keys mistakenly used as positions.
Related errors
- only 1D NumPy arrays can be treated as indices
- cannot treat Series of type {s.dtype} as indices
- cannot select columns using key of type {qualified_type_name
- cannot select rows using key of type {qualified_type_name(ke
- index positions should be smaller than 2^32
AI-assisted analysis of pola-rs/polars@df599052da (2026-08-16).
Data as JSON: /api/errors/79592b29a443e920.
Report an issue: GitHub.