{"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/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/functions/repeat.py#L205-L241","documentation":"Validation in pl.ones: the dtype must be numeric so a unit value (1) can be represented; _one_or_zero_by_dtype returns None for non-numeric/unsupported dtypes (e.g. String, List), triggering this TypeError.","triggerScenarios":"`pl.ones(n, dtype=pl.Utf8)` or other non-numeric dtypes (e.g. pl.List, pl.Struct, pl.Object, pl.Null).","commonSituations":"Passing a string dtype expecting '1' strings; generic code parameterized by dtype that can receive non-numeric dtypes.","solutions":["Use a numeric or boolean dtype: pl.Int64, pl.Float64, pl.Boolean, etc.","If you need string ones, build with `pl.repeat('1', n, dtype=pl.Utf8)` directly? — no; instead create numeric ones and cast: `pl.ones(n).cast(pl.Utf8)`","Validate dtype against a numeric allowlist before calling"],"exampleFix":"// before\npl.ones(3, dtype=pl.Utf8)\n// after\npl.ones(3, dtype=pl.Int64).cast(pl.Utf8)","handlingStrategy":"validation","validationCode":"NUMERIC = (pl.Int8, pl.Int16, pl.Int32, pl.Int64, pl.UInt8, pl.UInt16, pl.UInt32, pl.UInt64, pl.Float32, pl.Float64, pl.Boolean)\nif not (isinstance(dtype, pl.DataType) and any(dtype == d for d in NUMERIC)):\n    raise TypeError(f'ones requires numeric dtype, got {dtype}')","typeGuard":"def supports_ones(dtype) -> bool:\n    try:\n        from polars.functions.repeat import _one_or_zero_by_dtype\n        return _one_or_zero_by_dtype(1, dtype) is not None\n    except Exception:\n        return False","tryCatchPattern":"try:\n    s = pl.ones(n, dtype=dtype)\nexcept TypeError as e:\n    if 'invalid dtype for `ones`' in str(e):\n        s = pl.ones(n, dtype=pl.Int64).cast(dtype)\n    else:\n        raise","preventionTips":["Restrict dtype parameters of generic helpers to numeric types","Cast from Int64 ones instead of requesting unsupported dtypes"],"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"}