{"record":{"id":"091a12eba99b574e","repo":"pola-rs/polars","slug":"invalid-dtype-for-zeros-found-dtype","errorCode":null,"errorMessage":"invalid dtype for `zeros`; found {dtype}","messagePattern":"invalid dtype for `zeros`; found (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/functions/repeat.py","lineNumber":299,"sourceCode":"    See Also\n    --------\n    repeat\n    lit\n\n    Examples\n    --------\n    >>> pl.zeros(3, pl.Int8, eager=True)\n    shape: (3,)\n    Series: 'zeros' [i8]\n    [\n        0\n        0\n        0\n    ]\n    \"\"\"\n    if (zero := _one_or_zero_by_dtype(0, dtype)) is None:\n        msg = f\"invalid dtype for `zeros`; found {dtype}\"\n        raise TypeError(msg)\n\n    return repeat(zero, n=n, dtype=dtype, eager=eager).alias(\"zeros\")\n","sourceCodeStart":281,"sourceCodeEnd":302,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/functions/repeat.py#L281-L302","documentation":"The zeros() helper could not produce a zero literal for the requested dtype: _one_or_zero_by_dtype(0, dtype) returned None, meaning the dtype does not support a usable zero value (e.g. a non-numeric or otherwise incompatible type). Since the whole function is built around repeating a zero, it raises TypeError naming the offending dtype.","triggerScenarios":"`pl.zeros(n, dtype=pl.Utf8)` or any dtype where `_one_or_zero_by_dtype(0, dtype)` returns None.","commonSituations":"String/struct placeholders intended as 'zero'; parameterized helper functions fed arbitrary dtypes.","solutions":["Use a numeric or boolean dtype","Create numeric zeros and cast: `pl.zeros(n).cast(target_dtype)` if a cast exists","Gate the dtype with a numeric allowlist before the call"],"exampleFix":"// before\npl.zeros(3, dtype=pl.Utf8)\n// after\npl.zeros(3, dtype=pl.Int64).cast(pl.Utf8)","handlingStrategy":"validation","validationCode":"if dtype in (pl.Utf8, pl.Object, pl.Null, pl.List, pl.Struct):\n    raise TypeError(f'zeros requires numeric dtype, got {dtype}')","typeGuard":"def supports_zeros(dtype) -> bool:\n    try:\n        from polars.functions.repeat import _one_or_zero_by_dtype\n        return _one_or_zero_by_dtype(0, dtype) is not None\n    except Exception:\n        return False","tryCatchPattern":"try:\n    s = pl.zeros(n, dtype=dtype)\nexcept TypeError as e:\n    if 'invalid dtype for `zeros`' in str(e):\n        s = pl.zeros(n, dtype=pl.Int64).cast(dtype)\n    else:\n        raise","preventionTips":["Whitelist numeric dtypes in shared helpers","Prefer casting from a supported base dtype"],"tags":["python","polars","dtype","type-error"],"backgroundTag":"unsupported-dtype","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"}