{"record":{"id":"53496cbf6b8326d9","repo":"pola-rs/polars","slug":"invalid-dtype-t-r","errorCode":null,"errorMessage":"invalid dtype: {t!r}","messagePattern":"invalid dtype: (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/selectors.py","lineNumber":1086,"sourceCode":"    shape: (2, 2)\n    ┌───────┬──────────┐\n    │ other ┆ value    │\n    │ ---   ┆ ---      │\n    │ str   ┆ i64      │\n    ╞═══════╪══════════╡\n    │ bar   ┆ 5000555  │\n    │ foo   ┆ -3265500 │\n    └───────┴──────────┘\n    \"\"\"\n    all_dtypes: builtins.list[PolarsDataType | PythonDataType] = []\n    for tp in dtypes:\n        if is_polars_dtype(tp) or isinstance(tp, type):\n            all_dtypes.append(tp)\n        elif isinstance(tp, Collection):\n            for t in tp:\n                if not (is_polars_dtype(t) or isinstance(t, type)):\n                    msg = f\"invalid dtype: {t!r}\"\n                    raise TypeError(msg)\n                all_dtypes.append(t)\n        else:\n            msg = f\"invalid dtype: {tp!r}\"\n            raise TypeError(msg)\n\n    return Selector._by_dtype(all_dtypes)\n\n\ndef by_index(\n    *indices: int | range | Sequence[int | range], require_all: bool = True\n) -> Selector:\n    \"\"\"\n    Select all columns matching the given indices (or range objects).\n\n    Parameters\n    ----------\n    *indices\n        One or more column indices (or range objects).","sourceCodeStart":1068,"sourceCodeEnd":1104,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/selectors.py#L1068-L1104","documentation":"cs.by_dtype() validates every element of any collection passed to it: each must be a Polars dtype or a mappable builtin type. An element failing that check raises this TypeError naming the offending item. This is the per-element branch; the scalar branch is error 709.","triggerScenarios":"cs.by_dtype([pl.Int64, 3.14]) or cs.by_dtype((pl.String, 'utf8')) — a numeric, string, or None element inside the iterable.","commonSituations":"Dtype lists loaded from JSON/YAML where everything deserialized as strings; mixing pandas/numpy dtype objects into a polars dtype list; set literal containing a stray non-dtype value.","solutions":["Replace each invalid element with a polars dtype: 'float' -> pl.Float64.","Normalize with a mapping helper before the call: [DTYPES[d] if isinstance(d, str) else d for d in items].","Validate elements: all(is_polars_dtype(t) or isinstance(t, type) for t in items).","Convert foreign dtypes (pandas/numpy) to polars dtypes explicitly."],"exampleFix":"// before\ncs.by_dtype([pl.Int64, \"float\"])\n// after\ncs.by_dtype([pl.Int64, pl.Float64])","handlingStrategy":"validation","validationCode":"from polars.datatypes import is_polars_dtype\nbad = [t for t in dtype_collection if not (is_polars_dtype(t) or isinstance(t, type))]\nassert not bad, f\"invalid dtype elements: {bad!r}\"","typeGuard":"def all_dtype_like(items) -> bool:\n    from polars.datatypes import is_polars_dtype\n    return all(is_polars_dtype(t) or isinstance(t, type) for t in items)","tryCatchPattern":"try:\n    sel = cs.by_dtype(*dtype_collection)\nexcept TypeError as e:\n    if e.args and e.args[0].startswith(\"invalid dtype:\"):\n        dtype_collection = [DTYPE_MAP.get(t, t) for t in dtype_collection]\n        sel = cs.by_dtype(*dtype_collection)\n    else:\n        raise","preventionTips":["Deserialize config dtypes through a strict string->pl.dtype mapping.","Convert foreign dtype objects (pandas/numpy) to polars dtypes at boundaries.","Validate every element of dtype lists, not just the list."],"tags":["polars","selectors","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"}