pola-rs/polars · error
cannot treat NumPy array of type
Error message
cannot treat NumPy array of type {arr.dtype} as indices What it means
`_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.
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.
Example fix
// before arr = np.array([0.0, 2.0, 4.0]) sub = s[arr] # TypeError: cannot treat NumPy array of type float64... // after sub = s[arr.astype(np.int64)]
Defensive patterns
Strategy: type-guard
Validate before calling
if isinstance(idx, np.ndarray) and idx.dtype.kind not in ('i', 'u'):
if idx.dtype.kind == 'b':
raise ValueError("use .filter() for boolean masks")
idx = idx.astype(np.int64) Type guard
def is_int_ndarray(x) -> bool:
return isinstance(x, np.ndarray) and x.dtype.kind in ('i', 'u') Try / catch
try:
sub = s[np_idx]
except TypeError as e:
if "as indices" in str(e):
sub = s[np.asarray(np_idx).astype(np.int64)]
else:
raise Prevention
- Cast float index arrays to int before indexing.
- Use `//` instead of `/` for position arithmetic.
- Send boolean arrays to `.filter()`.
When it happens
Trigger: `s[np.array([0.0, 1.0])]`, `s[np.array(['a','b'])]`, or an index array produced by float division instead of integer division.
Common situations: 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.
Related errors
- cannot convert DataFrame to
- cannot convert List column
- cannot parse numpy data type
- cannot treat Series of type
- cannot use Series of dtype
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/79592b29a443e920.
Report an issue: GitHub.
Appendix: 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 fe841f959e)