{"record":{"id":"43a76aaa73894a80","repo":"pola-rs/polars","slug":"n-parameter-of-repeat-expected-a-int-or-expr","errorCode":null,"errorMessage":"`n` parameter of `repeat expected a `int` or `Expr` got a `{qualified_type_name(n)}`","messagePattern":"`n` parameter of `repeat expected a `int` or `Expr` got a `(.+?)`","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/functions/repeat.py","lineNumber":144,"sourceCode":"            \"z\"\n    ]\n\n    Generate a Series directly by setting `eager=True`.\n\n    >>> pl.repeat(3, n=3, dtype=pl.Int8, eager=True)\n    shape: (3,)\n    Series: 'repeat' [i8]\n    [\n            3\n            3\n            3\n    ]\n    \"\"\"\n    if isinstance(n, int):\n        n = F.lit(n)\n    if not hasattr(n, \"_pyexpr\"):\n        msg = f\"`n` parameter of `repeat expected a `int` or `Expr` got a `{qualified_type_name(n)}`\"\n        raise TypeError(msg)\n    value_pyexpr = parse_into_expression(value, str_as_lit=True, dtype=dtype)\n    expr = wrap_expr(plr.repeat(value_pyexpr, n._pyexpr, dtype))\n    if eager:\n        return F.select(expr).to_series()\n    return expr\n\n\n@overload\ndef ones(\n    n: int | Expr,\n    dtype: PolarsDataType = ...,\n    *,\n    eager: Literal[False] = ...,\n) -> Expr: ...\n\n\n@overload\ndef ones(","sourceCodeStart":126,"sourceCodeEnd":162,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/functions/repeat.py#L126-L162","documentation":"Raised by polars.repeat when the n parameter (after int is auto-wrapped into a literal) is neither an int nor an Expr — detected by the absence of the internal _pyexpr attribute. n must be a Python int or a polars expression so it can be passed to the Rust repeat kernel.","triggerScenarios":"pl.repeat(0, n=3.0) (float); n=None; n=pl.Series([3]) (Series is not accepted); n coming from a numpy integer or a config value that is not coerced to int.","commonSituations":"Passing a numpy int64 or a float count from downstream computation; passing a Series of lengths instead of an Expr (e.g. pl.repeat(v, n=df['len']) should be n=pl.col('len')); optional parameters defaulting to None.","solutions":["Coerce to int: n=int(my_count)","For per-row repeat lengths use an expression: pl.repeat(value, n=pl.col('len_col'))","If n arrives as a Series, use it in an expression context (pl.col) or extract a scalar with int(s[0])"],"exampleFix":"// before\npl.repeat(1, n=df['n'])   # Series -> error\n// after\npl.repeat(1, n=pl.col('n'))\n# scalar case: pl.repeat(1, n=int(count))","handlingStrategy":"type-guard","validationCode":"import polars as pl\n\nif not isinstance(n, (int, pl.Expr)):\n    n = int(n)  # or pl.lit(n) for expressions","typeGuard":"from polars import Expr\n\ndef is_valid_repeat_n(n) -> bool:\n    return isinstance(n, (int, Expr))","tryCatchPattern":"try:\n    e = pl.repeat(value, n=n)\nexcept TypeError:\n    e = pl.repeat(value, n=int(n))","preventionTips":["Coerce numpy ints and floats with int() before passing","Use pl.col('len') not df['len'] for per-row lengths"],"tags":["polars","repeat","type-validation","expression"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}