{"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: 'literal' [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/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/functions/repeat.py#L126-L162","documentation":"`pl.repeat`'s `n` must be an int or an Expr; after coercing ints, polars checks the value actually has an underlying `_pyexpr`. Any other type (float, str, list, None, numpy scalar treated as neither) fails this check.","triggerScenarios":"`pl.repeat(value, n=3.0)` (float), `n='3'` (str), `n=np.int64(3)` if not converted, or passing a Series/other object without `_pyexpr`.","commonSituations":"n coming from JSON/config as a float; passing a Python str thinking it will be parsed as an expression; numpy integer types.","solutions":["Pass `n` as a Python `int`, e.g. `n=3`, or wrap non-int scalars: `n=pl.lit(3)`","Convert numpy integers: `n=int(np_val)`","If n is dynamic, build an expression: `n=pl.col('len')` or `pl.lit(other_col)`"],"exampleFix":"// before\npl.repeat(0, n=5.0)\n// after\npl.repeat(0, n=5)  # or n=pl.lit(n_int)","handlingStrategy":"type-guard","validationCode":"if not isinstance(n, int) and not hasattr(n, '_pyexpr'):\n    raise TypeError(f'n must be int or Expr, got {type(n)}')","typeGuard":"def is_valid_n(n) -> bool:\n    return isinstance(n, int) or hasattr(n, '_pyexpr')","tryCatchPattern":"try:\n    expr = pl.repeat(value, n=n)\nexcept TypeError as e:\n    if '`n` parameter' in str(e):\n        expr = pl.repeat(value, n=pl.lit(int(n)))\n    else:\n        raise","preventionTips":["Coerce numeric n to int before calling (int(n))","Never pass raw strings/floats from config as n"],"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"}