{"record":{"id":"cec426344fd062b4","repo":"pola-rs/polars","slug":"invalid-index-value-idx-r","errorCode":null,"errorMessage":"invalid index value: {idx!r}","messagePattern":"invalid index value: (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/selectors.py","lineNumber":1194,"sourceCode":"    >>> df.select(~cs.by_index(range(1, 100, 2)))\n    shape: (1, 51)\n    ┌─────┬─────┬─────┬─────┬───┬──────┬──────┬──────┬──────┐\n    │ key ┆ c01 ┆ c03 ┆ c05 ┆ … ┆ c93  ┆ c95  ┆ c97  ┆ c99  │\n    │ --- ┆ --- ┆ --- ┆ --- ┆   ┆ ---  ┆ ---  ┆ ---  ┆ ---  │\n    │ str ┆ f64 ┆ f64 ┆ f64 ┆   ┆ f64  ┆ f64  ┆ f64  ┆ f64  │\n    ╞═════╪═════╪═════╪═════╪═══╪══════╪══════╪══════╪══════╡\n    │ abc ┆ 0.5 ┆ 1.5 ┆ 2.5 ┆ … ┆ 46.5 ┆ 47.5 ┆ 48.5 ┆ 49.5 │\n    └─────┴─────┴─────┴─────┴───┴──────┴──────┴──────┴──────┘\n    \"\"\"\n    all_indices: builtins.list[int] = []\n    for idx in indices:\n        if isinstance(idx, (range, Sequence)):\n            all_indices.extend(idx)  # type: ignore[arg-type]\n        elif isinstance(idx, int):\n            all_indices.append(idx)\n        else:\n            msg = f\"invalid index value: {idx!r}\"\n            raise TypeError(msg)\n\n    return Selector._from_pyselector(PySelector.by_index(all_indices, require_all))\n\n\ndef by_name(*names: str | Collection[str], require_all: bool = True) -> Selector:\n    \"\"\"\n    Select all columns matching the given names.\n\n    .. versionadded:: 0.20.27\n      The `require_all` parameter was added.\n\n    Parameters\n    ----------\n    *names\n        One or more names of columns to select.\n    require_all\n        Whether to match *all* names (the default) or *any* of the names.\n","sourceCodeStart":1176,"sourceCodeEnd":1212,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/selectors.py#L1176-L1212","documentation":"polars.selectors.by_index() raises this TypeError when an element passed as part of `idx` is not an int or a range/Sequence. Only integers (or collections/ranges of integers) are valid column indices for a selector; anything else (e.g. a string, float, or nested list) is rejected before building the PySelector.","triggerScenarios":"Calling cs.by_index(0, \"a\"), cs.by_index(1.5), cs.by_index([0, \"b\"]), or passing a nested structure like cs.by_index([[0,1]]) — any non-int element or non-Sequence/non-int top-level argument.","commonSituations":"Mixing up by_index with by_name and passing column names (strings); building indices programmatically where a None or float slipped in (e.g. from np.argpartition results or division); NumPy integer types are fine but Python floats from parsing config values are not.","solutions":["Convert all values to int: cs.by_index(*(int(i) for i in idx))","Check whether you meant cs.by_name() if you are passing column name strings","Filter/validate: idx = [i for i in idx if isinstance(i, int)] before calling","Cast NumPy scalar indices with .item() if needed"],"exampleFix":"// before\ncs.by_index(0, \"col_a\", 2.0)\n// after\ncs.by_index(0, 2)  # strings -> use cs.by_name(\"col_a\"); use int() on numerics","handlingStrategy":"type-guard","validationCode":"def valid_indices(idx):\n    if isinstance(idx, int) or isinstance(idx, (range, Sequence)):\n        items = [idx] if isinstance(idx, int) else list(idx)\n        return all(isinstance(i, int) for i in items)\n    return False\nassert valid_indices(my_idx)","typeGuard":"def is_index_value(x) -> bool:\n    return isinstance(x, int) or (isinstance(x, (range, Sequence)) and all(isinstance(i, int) for i in x))","tryCatchPattern":"try:\n    sel = cs.by_index(idx)\nexcept TypeError:\n    sel = cs.by_index(*(int(i) for i in idx if isinstance(i, (int, float))))","preventionTips":["Coerce numeric config values with int() before building selectors","Never mix column names into by_index; use by_name for strings","Watch for float indices from division or numpy float arrays — cast with int()/.item()"],"tags":["polars","selectors","type-error","indexing"],"backgroundTag":"invalid-argument-value","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"}