{"record":{"id":"53496cbf6b8326d9","repo":"pola-rs/polars","slug":"invalid-dtype-t-r","errorCode":null,"errorMessage":"invalid dtype: {t!r}","messagePattern":"invalid dtype: (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/selectors.py","lineNumber":1094,"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":1076,"sourceCodeEnd":1112,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/selectors.py#L1076-L1112","documentation":"Thrown inside cs.by_dtype (py-polars/src/polars/selectors.py:1094) when an ELEMENT of a collection argument is not a Polars DataType and not a plain Python type. by_dtype accepts dtypes, Python classes (e.g. int, str), and collections thereof; a non-dtype item inside a list/tuple (such as the string \"float\" or the int 5) fails this inner check with TypeError.","triggerScenarios":"cs.by_dtype([pl.Int64, \"float\"]) (string instead of pl.Float64), cs.by_dtype([pl.Int64, 5]), or cs.by_dtype([pl.Int64, None]). The OUTER item is a valid collection, but one element inside is neither a DataType nor a class.","commonSituations":"Passing dtype names as strings loaded from JSON/YAML config, or writing shorthand like \"str\"/\"float\" instead of pl.String/pl.Float64. Also mixing results of type(...) calls or None placeholders into dtype lists.","solutions":["Use actual Polars dtypes: cs.by_dtype([pl.Int64, pl.Float64]) — or Python classes: cs.by_dtype([int, float])","Map config strings to dtypes before calling: dtypes = [CONFIG_DTYPES[s] for s in config[\"dtypes\"]] with CONFIG_DTYPES = {\"int\": pl.Int64, \"float\": pl.Float64, ...}","Filter out invalid entries from dynamic lists: [d for d in lst if is_polars_dtype(d) or isinstance(d, type)]"],"exampleFix":"# before\nsel = cs.by_dtype([pl.Int64, \"float\"])  # \"float\" is a str, not a dtype\n\n# after\nsel = cs.by_dtype([pl.Int64, pl.Float64])\n# or with Python classes\nsel = cs.by_dtype([pl.Int64, float])","handlingStrategy":"type-guard","validationCode":"from polars import selectors as cs\n\ndef valid_dtype(d: object) -> bool:\n    import polars as pl\n    return pl.api.is_polars_dtype(d) or isinstance(d, type)\n\ndtypes = [d for d in config[\"dtypes\"] if valid_dtype(d)]\nsel = cs.by_dtype(dtypes)","typeGuard":"from typing import TypeGuard\n\ndef is_dtype_arg(item: object) -> TypeGuard[object]:\n    import polars as pl\n    return pl.api.is_polars_dtype(item) or isinstance(item, type)","tryCatchPattern":"try:\n    sel = cs.by_dtype(raw_list)\nexcept TypeError as e:\n    if \"invalid dtype\" in str(e):\n        bad = [d for d in raw_list if not (pl.api.is_polars_dtype(d) or isinstance(d, type))]\n        raise ValueError(f\"invalid dtypes in config: {bad!r}\") from e\n    raise","preventionTips":["Map config strings to pl.* dtypes at load time instead of passing them raw","Use pl.String/pl.Float64 rather than 'str'/'float' strings"],"tags":["polars","selectors","typeerror","dtype","column-selection"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}