pola-rs/polars · error · TypeError

invalid dtype for `zeros`; found {dtype}

Error message

invalid dtype for `zeros`; found {dtype}

What it means

Raised by pl.zeros when the requested dtype has no sensible 'zero' representation. Supported dtypes are the integer and float families, Boolean, String (Utf8), Decimal, and List/Array of those (same lookup as ones); any other dtype (Date, Datetime, Duration, Categorical, ...) fails.

Source

Thrown at py-polars/src/polars/functions/repeat.py:299

    See Also
    --------
    repeat
    lit

    Examples
    --------
    >>> pl.zeros(3, pl.Int8, eager=True)
    shape: (3,)
    Series: 'zeros' [i8]
    [
        0
        0
        0
    ]
    """
    if (zero := _one_or_zero_by_dtype(0, dtype)) is None:
        msg = f"invalid dtype for `zeros`; found {dtype}"
        raise TypeError(msg)

    return repeat(zero, n=n, dtype=dtype, eager=eager).alias("zeros")

View on GitHub (pinned to df599052da)

Solutions

  1. Create zeros with a supported dtype then cast: pl.zeros(3, pl.Int64).cast(pl.Datetime('us'))
  2. Use pl.repeat(0, n, dtype=...) or pl.repeat(value, ...) for values polars can type
  3. For temporals, prefer pl.repeat + cast or fill_null on a literal expression

Example fix

// before
pl.zeros(3, pl.Datetime('us'), eager=True)
// after
pl.zeros(3, pl.Int64, eager=True).cast(pl.Datetime('us'))
Defensive patterns

Strategy: validation

Validate before calling

from polars.datatypes.group import INTEGER_DTYPES, FLOAT_DTYPES
from polars import Boolean, Utf8, Decimal, List, Array

zeros_ok = 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.zeros(3, dtype, eager=True)
except TypeError:
    s = pl.zeros(3, pl.Int64, eager=True).cast(dtype)

Prevention

When it happens

Trigger: pl.zeros(3, pl.Datetime('us')); pl.zeros(3, dtype=pl.Duration); pl.zeros(3, pl.Enum(['a','b'])); List inner dtype that is temporal.

Common situations: Prealloculating null-free placeholder columns for temporal schemas; masking idioms ported from numpy; writing generic 'make empty-ish column of dtype X' helpers that route through zeros.

Related errors


AI-assisted analysis of pola-rs/polars@df599052da (2026-08-16). Data as JSON: /api/errors/091a12eba99b574e. Report an issue: GitHub.