pola-rs/polars · error

column index {original_index} is out of range (frame has {se

Error message

column index {original_index} is out of range (frame has {self.width} columns)

What it means

Error "column index {original_index} is out of range (frame has {self.width} columns)" thrown in pola-rs/polars.

Source

Thrown at py-polars/src/polars/dataframe/frame.py:5372

        ... )
        >>> expr = (pl.col("b") / pl.col("a")).alias("b_div_a")
        >>> df.insert_column(2, expr)
        shape: (3, 4)
        ┌─────┬──────┬─────────┬─────┐
        │ a   ┆ b    ┆ b_div_a ┆ c   │
        │ --- ┆ ---  ┆ ---     ┆ --- │
        │ i64 ┆ f64  ┆ f64     ┆ str │
        ╞═════╪══════╪═════════╪═════╡
        │ 2   ┆ 0.5  ┆ 0.25    ┆ xx  │
        │ 4   ┆ 4.0  ┆ 1.0     ┆ yy  │
        │ 2   ┆ 10.0 ┆ 5.0     ┆ zz  │
        └─────┴──────┴─────────┴─────┘
        """
        if (original_index := index) < 0:
            index = self.width + index
            if index < 0:
                msg = f"column index {original_index} is out of range (frame has {self.width} columns)"
                raise IndexError(msg)
        elif index > self.width:
            msg = f"column index {original_index} is out of range (frame has {self.width} columns)"
            raise IndexError(msg)

        if isinstance(column, pl.Series):
            self._df.insert_column(index, column._s)
        else:
            if isinstance(column, str):
                column = F.col(column)
            if isinstance(column, pl.Expr):
                cols = self.columns
                cols.insert(index, column)  # type: ignore[arg-type]
                self._df = self.select(cols)._df
            else:
                msg = f"column must be a Series or Expr, got {column!r} (type={qualified_type_name(column)})"
                raise TypeError(msg)
        return self

View on GitHub (pinned to df599052da)

When it happens

Trigger: Thrown at py-polars/src/polars/dataframe/frame.py:5372 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of pola-rs/polars@df599052da (2026-08-16). Data as JSON: /api/errors/8c6e90504c3ef995. Report an issue: GitHub.