pola-rs/polars · error · TypeError
`n` parameter of `repeat expected a `int` or `Expr` got a `{
Error message
`n` parameter of `repeat expected a `int` or `Expr` got a `{qualified_type_name(n)}` What it means
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.
Source
Thrown at py-polars/src/polars/functions/repeat.py:144
"z"
]
Generate a Series directly by setting `eager=True`.
>>> pl.repeat(3, n=3, dtype=pl.Int8, eager=True)
shape: (3,)
Series: 'repeat' [i8]
[
3
3
3
]
"""
if isinstance(n, int):
n = F.lit(n)
if not hasattr(n, "_pyexpr"):
msg = f"`n` parameter of `repeat expected a `int` or `Expr` got a `{qualified_type_name(n)}`"
raise TypeError(msg)
value_pyexpr = parse_into_expression(value, str_as_lit=True, dtype=dtype)
expr = wrap_expr(plr.repeat(value_pyexpr, n._pyexpr, dtype))
if eager:
return F.select(expr).to_series()
return expr
@overload
def ones(
n: int | Expr,
dtype: PolarsDataType = ...,
*,
eager: Literal[False] = ...,
) -> Expr: ...
@overload
def ones(View on GitHub (pinned to df599052da)
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])
Example fix
// before
pl.repeat(1, n=df['n']) # Series -> error
// after
pl.repeat(1, n=pl.col('n'))
# scalar case: pl.repeat(1, n=int(count)) Defensive patterns
Strategy: type-guard
Validate before calling
import polars as pl
if not isinstance(n, (int, pl.Expr)):
n = int(n) # or pl.lit(n) for expressions Type guard
from polars import Expr
def is_valid_repeat_n(n) -> bool:
return isinstance(n, (int, Expr)) Try / catch
try:
e = pl.repeat(value, n=n)
except TypeError:
e = pl.repeat(value, n=int(n)) Prevention
- Coerce numpy ints and floats with int() before passing
- Use pl.col('len') not df['len'] for per-row lengths
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- expected `by_predicate` to be an expression, got {qualified_
- expected Series in 'arg_where' if 'eager=True', got {type(co
- cannot turn {qualified_type_name(input)!r} into selector
- Cannot pass a dictionary as a single positional argument.\nI
- expected `on` to be str or Expr, got {qualified_type_name(on
AI-assisted analysis of pola-rs/polars@df599052da (2026-08-16).
Data as JSON: /api/errors/43a76aaa73894a80.
Report an issue: GitHub.