{"record":{"id":"6427b3c2a9ad02b1","repo":"pola-rs/polars","slug":"invalid-dtype-for-ones-found-dtype","errorCode":null,"errorMessage":"invalid dtype for `ones`; found {dtype}","messagePattern":"invalid dtype for `ones`; found (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/functions/repeat.py","lineNumber":223,"sourceCode":"    See Also\n    --------\n    repeat\n    lit\n\n    Examples\n    --------\n    >>> pl.ones(3, pl.Int8, eager=True)\n    shape: (3,)\n    Series: 'ones' [i8]\n    [\n        1\n        1\n        1\n    ]\n    \"\"\"\n    if (one := _one_or_zero_by_dtype(1, dtype)) is None:\n        msg = f\"invalid dtype for `ones`; found {dtype}\"\n        raise TypeError(msg)\n\n    return repeat(one, n=n, dtype=dtype, eager=eager).alias(\"ones\")\n\n\n@overload\ndef zeros(\n    n: int | Expr,\n    dtype: PolarsDataType = ...,\n    *,\n    eager: Literal[False] = ...,\n) -> Expr: ...\n\n\n@overload\ndef zeros(\n    n: int | Expr,\n    dtype: PolarsDataType = ...,\n    *,","sourceCodeStart":205,"sourceCodeEnd":241,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/functions/repeat.py#L205-L241","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\npl.ones(3, pl.Date, eager=True)\n// after\npl.ones(3, pl.Int64, eager=True).cast(pl.Date)","handlingStrategy":"validation","validationCode":"from polars.datatypes.group import FLOAT_DTYPES, INTEGER_DTYPES\nfrom polars import Boolean, Utf8, Decimal, List, Array\n\ndef ones_supported(dtype) -> bool:\n    return (dtype in INTEGER_DTYPES or dtype in FLOAT_DTYPES\n            or dtype in (Boolean, Utf8) or isinstance(dtype, (Decimal, List, Array)))","typeGuard":"def is_fillable_dtype(dtype) -> bool:\n    from polars.datatypes.group import INTEGER_DTYPES, FLOAT_DTYPES\n    from polars import Boolean, Utf8, Decimal, List, Array\n    return (dtype in INTEGER_DTYPES or dtype in FLOAT_DTYPES\n            or dtype in (Boolean, Utf8) or isinstance(dtype, (Decimal, List, Array)))","tryCatchPattern":"try:\n    s = pl.ones(3, dtype, eager=True)\nexcept TypeError:\n    s = pl.ones(3, pl.Int64, eager=True).cast(dtype)","preventionTips":["Route 'constant column of dtype X' helpers through pl.repeat + cast","Unit-test helper functions against temporal and categorical dtypes"],"tags":["polars","ones","dtype-validation","initialization"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}