{"record":{"id":"7da9d3e67fa75f1c","repo":"pola-rs/polars","slug":"offset-must-be-an-integer-string-or-expression","errorCode":null,"errorMessage":"'offset' must be an integer, string, or expression, not {type(offset).__name__}","messagePattern":"'offset' must be an integer, string, or expression, not (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/expr/list.py","lineNumber":1127,"sourceCode":"            end of the list.\n\n        Examples\n        --------\n        >>> df = pl.DataFrame({\"a\": [[1, 2, 3, 4], [10, 2, 1]]})\n        >>> df.with_columns(slice=pl.col(\"a\").list.slice(1, 2))\n        shape: (2, 2)\n        ┌─────────────┬───────────┐\n        │ a           ┆ slice     │\n        │ ---         ┆ ---       │\n        │ list[i64]   ┆ list[i64] │\n        ╞═════════════╪═══════════╡\n        │ [1, 2, … 4] ┆ [2, 3]    │\n        │ [10, 2, 1]  ┆ [2, 1]    │\n        └─────────────┴───────────┘\n        \"\"\"\n        if isinstance(offset, Collection) and not isinstance(offset, str):\n            msg = f\"'offset' must be an integer, string, or expression, not {type(offset).__name__}\"\n            raise TypeError(msg)\n        if (\n            length is not None\n            and isinstance(length, Collection)\n            and not isinstance(length, str)\n        ):\n            msg = f\"'length' must be an integer, string, or expression, not {type(length).__name__}\"\n            raise TypeError(msg)\n\n        offset_pyexpr = parse_into_expression(offset)\n        length_pyexpr = parse_into_expression(length)\n        return wrap_expr(self._pyexpr.list_slice(offset_pyexpr, length_pyexpr))\n\n    def head(self, n: int | str | Expr = 5) -> Expr:\n        \"\"\"\n        Slice the first `n` values of every sub-list.\n\n        .. engine-support:: in-memory, streaming, distributed\n","sourceCodeStart":1109,"sourceCodeEnd":1145,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/expr/list.py#L1109-L1145","documentation":"Expr.list.slice() validates its `offset` argument: it may be an integer, a string column name, or an Expr, but not an arbitrary collection (list/tuple/dict/set). Passing a Collection raises this TypeError immediately in Python before any expression is built. Polars raises it early so the mistake surfaces at call time instead of deep in query evaluation.","triggerScenarios":"Calling Expr.list.slice(offset, length) with offset given as a list, tuple, set, or dict (e.g. df.select(pl.col('a').list.slice([0, 2]))) instead of an int/str/Expr.","commonSituations":"Copy-pasting a slice range like [0, 5] as the offset; confusing Python slicing with list.slice's (offset, length) signature; programmatically building arguments and accidentally passing a list.","solutions":["Pass the offset as a single integer: list.slice(0, 2).","If you intended a range [start, end], convert it: list.slice(start, end - start).","If offset should come from a column, pass its name as a str or a pl.col(...) expression."],"exampleFix":"// before\npl.col('items').list.slice([0, 3])\n// after\npl.col('items').list.slice(0, 3)","handlingStrategy":"type-guard","validationCode":"if not isinstance(offset, (int, str, pl.Expr)):\n    raise TypeError(f'offset must be int/str/Expr, got {type(offset).__name__}')","typeGuard":"def is_valid_offset(v) -> bool:\n    return isinstance(v, (int, str, pl.Expr))","tryCatchPattern":"try:\n    expr = pl.col('a').list.slice(offset, length)\nexcept TypeError:\n    expr = pl.col('a').list.slice(int(offset[0]), length)","preventionTips":["Remember list.slice takes (offset, length), not a (start, end) pair.","Validate arguments are int/str/Expr before building expressions.","Add unit tests for expression-building helpers with edge-case arguments."],"tags":["python","polars","type-error","arguments"],"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"}