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
`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.
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)`
Example fix
// before pl.repeat(0, n=5.0) // after pl.repeat(0, n=5) # or n=pl.lit(n_int)
Defensive patterns
Strategy: type-guard
Validate before calling
if not isinstance(n, int) and not hasattr(n, '_pyexpr'):
raise TypeError(f'n must be int or Expr, got {type(n)}') Type guard
def is_valid_n(n) -> bool:
return isinstance(n, int) or hasattr(n, '_pyexpr') Try / catch
try:
expr = pl.repeat(value, n=n)
except TypeError as e:
if '`n` parameter' in str(e):
expr = pl.repeat(value, n=pl.lit(int(n)))
else:
raise Prevention
- Coerce numeric n to int before calling (int(n))
- Never pass raw strings/floats from config as n
When it happens
Trigger: `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`.
Common situations: n coming from JSON/config as a float; passing a Python str thinking it will be parsed as an expression; numpy integer types.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- 'length' must be an integer, string, or expression, not
- list.to_struct() got a str instead of a list. hint: pass
- 'offset' must be an integer, string, or expression, not
- "pad_end" expects a `str`, given a
- "pad_start" expects a `str`, given a
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/43a76aaa73894a80.
Report an issue: GitHub.
Appendix: 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: 'literal' [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 fe841f959e)