{"record":{"id":"6497657145aa34e2","repo":"pola-rs/polars","slug":"invalid-dtype-tp-r","errorCode":null,"errorMessage":"invalid dtype: {tp!r}","messagePattern":"invalid dtype: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/selectors.py","lineNumber":1098,"sourceCode":"    │ 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).\n        Negative indexing is supported.\n    require_all\n        By default, all specified indices must be valid; if any index is out of bounds,\n        an error is raised. If set to `False`, out-of-bounds indices are ignored","sourceCodeStart":1080,"sourceCodeEnd":1116,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/selectors.py#L1080-L1116","documentation":"Thrown inside cs.by_dtype (py-polars/src/polars/selectors.py:1098) when a TOP-LEVEL argument is not a DataType, not a Python class, and not a Collection. by_dtype(*dtypes) flattens collections and validates each item; an outer item that is itself invalid (an int, a string, None, a DataFrame column object, etc.) hits this else-branch and raises TypeError.","triggerScenarios":"cs.by_dtype(5), cs.by_dtype(\"float\"), cs.by_dtype(None), or cs.by_dtype(pl.Int64, 3) where the second positional arg is a scalar non-dtype.","commonSituations":"Passing a single dtype name as a string (\"numeric\") instead of a dtype object, passing a variable that was never validated (often None from an optional config), or passing an index/position by mistake.","solutions":["Pass Polars dtypes or Python classes: cs.by_dtype(pl.Int64) or cs.by_dtype(int, float)","Guard optional config: only call cs.by_dtype(*dtypes) when all items are dtypes/classes; skip or default when the list is empty","Convert string dtype names to pl.* dtypes before the call (see by_dtype docs for the accepted forms)"],"exampleFix":"# before\nsel = cs.by_dtype(\"float\")  # str is not a dtype\n\n# after\nsel = cs.by_dtype(pl.Float64)\n# or\nsel = cs.by_dtype(float)","handlingStrategy":"type-guard","validationCode":"import polars as pl\n\ndef to_dtype_args(items):\n    out = []\n    for it in items:\n        if pl.api.is_polars_dtype(it) or isinstance(it, type):\n            out.append(it)\n        elif isinstance(it, str):\n            out.append(getattr(pl, it))  # 'Int64' -> pl.Int64\n        else:\n            raise TypeError(f\"invalid dtype: {it!r}\")\n    return out","typeGuard":"def is_by_dtype_arg(item: object) -> bool:\n    import polars as pl\n    return pl.api.is_polars_dtype(item) or isinstance(item, type)","tryCatchPattern":"try:\n    sel = cs.by_dtype(*args)\nexcept TypeError as e:\n    if \"invalid dtype\" in str(e):\n        # fall back to per-item validation with a clearer message\n        for a in args:\n            assert pl.api.is_polars_dtype(a) or isinstance(a, type), f\"bad dtype {a!r}\"\n    raise","preventionTips":["Validate optional dtype lists before calling by_dtype; never pass unvalidated config values","Remember both pl.Int64 and the Python class int are accepted; strings never are"],"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"}