{"record":{"id":"ce6101e1c0cbf38b","repo":"pola-rs/polars","slug":"index-positions-should-be-smaller-than-2-32","errorCode":null,"errorMessage":"index positions should be smaller than 2^32","messagePattern":"index positions should be smaller than 2\\^32","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/_utils/getitem.py","lineNumber":371,"sourceCode":"    idx_type = get_index_type()\n\n    if s.dtype == idx_type:\n        return s\n\n    if not s.dtype.is_integer():\n        if s.dtype == Boolean:\n            _raise_on_boolean_mask()\n        else:\n            msg = f\"cannot treat Series of type {s.dtype} as indices\"\n            raise TypeError(msg)\n\n    if s.len() == 0:\n        return pl.Series(s.name, [], dtype=idx_type)\n\n    if idx_type == UInt32:\n        if s.dtype in {Int64, UInt64} and s.max() >= U32_MAX:  # type: ignore[operator]\n            msg = \"index positions should be smaller than 2^32\"\n            raise ValueError(msg)\n        if s.dtype == Int64 and s.min() < -U32_MAX:  # type: ignore[operator]\n            msg = \"index positions should be greater than or equal to -2^32\"\n            raise ValueError(msg)\n\n    if s.dtype.is_signed_integer():\n        if s.min() < 0:  # type: ignore[operator]\n            if idx_type == UInt32:\n                idxs = s.cast(Int32) if s.dtype in {Int8, Int16} else s\n            else:\n                idxs = s.cast(Int64) if s.dtype in {Int8, Int16, Int32} else s\n\n            # Update negative indexes to absolute indexes.\n            return (\n                idxs.to_frame()\n                .select(\n                    F.when(F.col(idxs.name) < 0)\n                    .then(size + F.col(idxs.name))\n                    .otherwise(F.col(idxs.name))","sourceCodeStart":353,"sourceCodeEnd":389,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/_utils/getitem.py#L353-L389","documentation":"When the default index type is UInt32 (polars' default), `_convert_series_to_indices` checks the max of an Int64/UInt64 index Series against 2^32-1 and raises ValueError if any position exceeds it, because positions cannot be represented in UInt32.","triggerScenarios":"Indexing with a pl.Series of dtype Int64 or UInt64 whose max value is >= 4294967295, e.g. `s[pl.Series([2**32, 5])]`.","commonSituations":"Building index Series from large computed values (counters, hashes, offsets) on a DataFrame with billions of rows' worth of address space, or reusing ID columns (like user IDs) as positional indices.","solutions":["Ensure index positions are < 2^32; select a subrange of the frame first, then index.","Run polars with `POLARS_MAX_THREADS`/index type as u64 if available (`polars.Config` / `pl.enable_string_cache`-style env `POLARS_USING_U64_INDEX` is not public — prefer keeping positions in range).","Convert the selection to a boolean mask or `.take()` after shrinking data instead of passing huge positions."],"exampleFix":"// before\nbig = pl.int_range(0, n).filter(ids > 2**32)\nsub = s[big]  # ValueError: index positions should be smaller than 2^32\n// after\nsub_small = s.filter(pl.int_range(0, s.len()) < 2**32)\nsub = sub_small.take(big.filter(big < 2**32))","handlingStrategy":"validation","validationCode":"if idx.dtype in (pl.Int64, pl.UInt64) and idx.len() and idx.max() >= 2**32 - 1:\n    raise ValueError(\"index positions must be < 2^32\")","typeGuard":"def fits_u32(idx: pl.Series) -> bool:\n    return idx.dtype not in (pl.Int64, pl.UInt64) or idx.max() < 2**32 - 1","tryCatchPattern":"try:\n    sub = s[idx]\nexcept ValueError as e:\n    if \"smaller than 2^32\" in str(e):\n        sub = s[idx.filter(idx < 2**32 - 1)]\n    else:\n        raise","preventionTips":["Bound index values when computing them; slice data before indexing large frames.","Don't reuse ID columns as positional indices.","Prefer `.filter()`/`.gather()` with validated masks."],"tags":["polars","indexing","valueerror","range"],"backgroundTag":"value-out-of-range","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"}