pola-rs/polars · error · TypeError
invalid dtype for `ones`; found {dtype}
Error message
invalid dtype for `ones`; found {dtype} What it means
Raised by pl.ones when the requested dtype has no sensible 'one' representation. The helper _one_or_zero_by_dtype only supports integer dtypes, float dtypes, Boolean, String (Utf8), Decimal, and List/Array of those; anything else returns None and triggers this TypeError.
Source
Thrown at py-polars/src/polars/functions/repeat.py:223
See Also
--------
repeat
lit
Examples
--------
>>> pl.ones(3, pl.Int8, eager=True)
shape: (3,)
Series: 'ones' [i8]
[
1
1
1
]
"""
if (one := _one_or_zero_by_dtype(1, dtype)) is None:
msg = f"invalid dtype for `ones`; found {dtype}"
raise TypeError(msg)
return repeat(one, n=n, dtype=dtype, eager=eager).alias("ones")
@overload
def zeros(
n: int | Expr,
dtype: PolarsDataType = ...,
*,
eager: Literal[False] = ...,
) -> Expr: ...
@overload
def zeros(
n: int | Expr,
dtype: PolarsDataType = ...,
*,View on GitHub (pinned to df599052da)
Solutions
- Use a supported dtype (Int/Float/Boolean/String/Decimal or List/Array of those) then .cast() to the target dtype: pl.ones(3, pl.Int64).cast(pl.Date)
- For arbitrary literal fills, use pl.repeat(value, n, dtype=...) which accepts a broader value/dtype combination
- For datetimes, build from ones on integers: (pl.ones(3, pl.Int64) * 0).cast(Datatype) or pl.repeat + cast
Example fix
// before pl.ones(3, pl.Date, eager=True) // after pl.ones(3, pl.Int64, eager=True).cast(pl.Date)
Defensive patterns
Strategy: validation
Validate before calling
from polars.datatypes.group import FLOAT_DTYPES, INTEGER_DTYPES
from polars import Boolean, Utf8, Decimal, List, Array
def ones_supported(dtype) -> bool:
return (dtype in INTEGER_DTYPES or dtype in FLOAT_DTYPES
or dtype in (Boolean, Utf8) or isinstance(dtype, (Decimal, List, Array))) Type guard
def is_fillable_dtype(dtype) -> bool:
from polars.datatypes.group import INTEGER_DTYPES, FLOAT_DTYPES
from polars import Boolean, Utf8, Decimal, List, Array
return (dtype in INTEGER_DTYPES or dtype in FLOAT_DTYPES
or dtype in (Boolean, Utf8) or isinstance(dtype, (Decimal, List, Array))) Try / catch
try:
s = pl.ones(3, dtype, eager=True)
except TypeError:
s = pl.ones(3, pl.Int64, eager=True).cast(dtype) Prevention
- Route 'constant column of dtype X' helpers through pl.repeat + cast
- Unit-test helper functions against temporal and categorical dtypes
When it happens
Trigger: pl.ones(3, pl.Date); pl.ones(3, dtype=Datetime('us')); pl.ones(3, pl.Categorical); pl.ones(3, pl.Duration('ms')); nested List of an unsupported inner dtype.
Common situations: Trying to preallocate typed dummy columns for schemas that include temporal/categorical dtypes; porting numpy.ones-style initialization to polars; assuming any dtype works for a constant fill.
Related errors
- invalid dtype for `zeros`; found {dtype}
- cannot select columns using key of type {qualified_type_name
- expected {df.width} values when selecting columns by boolean
- index {key} is out of bounds for DataFrame of height {num_ro
- cannot select rows using key of type {qualified_type_name(ke
AI-assisted analysis of pola-rs/polars@df599052da (2026-08-16).
Data as JSON: /api/errors/6427b3c2a9ad02b1.
Report an issue: GitHub.