{"record":{"id":"d70f211098af9e2c","repo":"pola-rs/polars","slug":"offset-input-for-with-row-index-cannot-be-iss","errorCode":null,"errorMessage":"`offset` input for `with_row_index` cannot be {issue}, got {offset}","messagePattern":"`offset` input for `with_row_index` cannot be (.+?), got (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/dataframe/frame.py","lineNumber":7124,"sourceCode":"        ...     pl.all(),\n        ... )\n        shape: (3, 3)\n        ┌───────┬─────┬─────┐\n        │ index ┆ a   ┆ b   │\n        │ ---   ┆ --- ┆ --- │\n        │ u32   ┆ i64 ┆ i64 │\n        ╞═══════╪═════╪═════╡\n        │ 0     ┆ 1   ┆ 2   │\n        │ 1     ┆ 3   ┆ 4   │\n        │ 2     ┆ 5   ┆ 6   │\n        └───────┴─────┴─────┘\n        \"\"\"\n        try:\n            return self._from_pydf(self._df.with_row_index(name, offset))\n        except OverflowError:\n            issue = \"negative\" if offset < 0 else \"greater than the maximum index value\"\n            msg = f\"`offset` input for `with_row_index` cannot be {issue}, got {offset}\"\n            raise ValueError(msg) from None\n\n    @deprecated(\n        \"`DataFrame.with_row_count` is deprecated; use `with_row_index` instead.\"\n        \" Note that the default column name has changed from 'row_nr' to 'index'.\"\n    )\n    def with_row_count(self, name: str = \"row_nr\", offset: int = 0) -> DataFrame:\n        \"\"\"\n        Add a column at index 0 that counts the rows.\n\n        .. deprecated:: 0.20.4\n            Use the :meth:`with_row_index` method instead.\n            Note that the default column name has changed from 'row_nr' to 'index'.\n\n        Parameters\n        ----------\n        name\n            Name of the column to add.\n        offset","sourceCodeStart":7106,"sourceCodeEnd":7142,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/dataframe/frame.py#L7106-L7142","documentation":"DataFrame.with_row_index(name, offset) adds a row counter starting at `offset`. The offset is converted to the internal unsigned index dtype (u32 in standard builds, u64 with the bigidx feature) on the Rust side; a value that cannot be represented — negative, or above the maximum for that integer type — raises OverflowError, which this wrapper re-raises as ValueError with a 'negative' or 'greater than the maximum index value' detail.","triggerScenarios":"df.with_row_index('idx', offset=-1) (any negative offset); df.with_row_index('idx', offset=2**32) on a standard u32-index build; offsets computed as len(prev_frames) - len(df) that go negative; offsets sourced from configuration or a data file.","commonSituations":"Emulating SQL ROW_NUMBER() with a non-zero or negative start; continuing numbering across concatenated frames where an intermediate frame is longer than expected; reusing slicing offsets (which may be negative) as with_row_index offsets.","solutions":["Use a non-negative offset that fits the index dtype (0 to 2**32-1 on standard builds)","For an arbitrary (including negative) start, add an expression instead: df.with_row_index('index').with_columns((pl.col('index') + start).alias('index')) — cast to Int64 if start is negative or large","Fix the arithmetic that produced the offset, e.g. clamp with max(0, total_so_far)","When continuing numbering across frames, accumulate offsets as a running total of preceding frame heights (always >= 0)"],"exampleFix":"# before\ndf = df.with_row_index('idx', offset=-10)\n\n# after\ndf = (\n    df.with_row_index('idx')\n      .with_columns((pl.col('idx').cast(pl.Int64) - 10).alias('idx'))\n)","handlingStrategy":"validation","validationCode":"MAX_OFFSET = 2**32 - 1  # standard u32 index builds; 2**64 - 1 with bigidx\nif not (0 <= offset <= MAX_OFFSET):\n    raise ValueError(f'offset {offset} out of range [0, {MAX_OFFSET}]')\ndf = df.with_row_index('index', offset=offset)","typeGuard":"def is_valid_row_index_offset(offset: int) -> bool:\n    return isinstance(offset, int) and not isinstance(offset, bool) and 0 <= offset <= 2**32 - 1","tryCatchPattern":"try:\n    df = df.with_row_index('idx', offset)\nexcept ValueError as e:\n    if '`offset` input' not in str(e):\n        raise\n    # negative or out-of-range start: emulate via expression instead\n    df = df.with_row_index('idx').with_columns((pl.col('idx').cast(pl.Int64) + offset).alias('idx'))","preventionTips":["Treat offsets as unsigned 32-bit quantities by contract; reject negatives at your API boundary","Compute cross-frame offsets as running sums of preceding heights (never len(a) - len(b))","For arbitrary starts, prefer with_row_index(0) plus an integer add/cast — it has no overflow constraints"],"tags":["polars","dataframe","with-row-index","overflow","numeric-limits","valueerror"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}