{"record":{"id":"8c6e90504c3ef995","repo":"pola-rs/polars","slug":"column-index-original-index-is-out-of-range-fra","errorCode":null,"errorMessage":"column index {original_index} is out of range (frame has {self.width} columns)","messagePattern":"column index (.+?) is out of range \\(frame has (.+?) columns\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/dataframe/frame.py","lineNumber":5372,"sourceCode":"        ... )\n        >>> expr = (pl.col(\"b\") / pl.col(\"a\")).alias(\"b_div_a\")\n        >>> df.insert_column(2, expr)\n        shape: (3, 4)\n        ┌─────┬──────┬─────────┬─────┐\n        │ a   ┆ b    ┆ b_div_a ┆ c   │\n        │ --- ┆ ---  ┆ ---     ┆ --- │\n        │ i64 ┆ f64  ┆ f64     ┆ str │\n        ╞═════╪══════╪═════════╪═════╡\n        │ 2   ┆ 0.5  ┆ 0.25    ┆ xx  │\n        │ 4   ┆ 4.0  ┆ 1.0     ┆ yy  │\n        │ 2   ┆ 10.0 ┆ 5.0     ┆ zz  │\n        └─────┴──────┴─────────┴─────┘\n        \"\"\"\n        if (original_index := index) < 0:\n            index = self.width + index\n            if index < 0:\n                msg = f\"column index {original_index} is out of range (frame has {self.width} columns)\"\n                raise IndexError(msg)\n        elif index > self.width:\n            msg = f\"column index {original_index} is out of range (frame has {self.width} columns)\"\n            raise IndexError(msg)\n\n        if isinstance(column, pl.Series):\n            self._df.insert_column(index, column._s)\n        else:\n            if isinstance(column, str):\n                column = F.col(column)\n            if isinstance(column, pl.Expr):\n                cols = self.columns\n                cols.insert(index, column)  # type: ignore[arg-type]\n                self._df = self.select(cols)._df\n            else:\n                msg = f\"column must be a Series or Expr, got {column!r} (type={qualified_type_name(column)})\"\n                raise TypeError(msg)\n        return self\n","sourceCodeStart":5354,"sourceCodeEnd":5390,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/dataframe/frame.py#L5354-L5390","documentation":"Error \"column index {original_index} is out of range (frame has {self.width} columns)\" thrown in pola-rs/polars.","triggerScenarios":"Thrown at py-polars/src/polars/dataframe/frame.py:5372 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}