{"record":{"id":"6a5303146bd11ef7","repo":"pola-rs/polars","slug":"invalid-name-nm-r","errorCode":null,"errorMessage":"invalid name: {nm!r}","messagePattern":"invalid name: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/selectors.py","lineNumber":1295,"sourceCode":"    │ f64 ┆ bool  │\n    ╞═════╪═══════╡\n    │ 2.0 ┆ false │\n    │ 5.5 ┆ true  │\n    └─────┴───────┘\n    \"\"\"\n    all_names = []\n    for nm in names:\n        if isinstance(nm, str):\n            all_names.append(nm)\n        elif isinstance(nm, Collection):\n            for n in nm:\n                if not isinstance(n, str):\n                    msg = f\"invalid name: {n!r}\"\n                    raise TypeError(msg)\n                all_names.append(n)\n        else:\n            msg = f\"invalid name: {nm!r}\"\n            raise TypeError(msg)\n\n    return Selector._by_name(all_names, strict=require_all, expand_patterns=False)\n\n\ndef empty() -> Selector:\n    \"\"\"\n    Select no columns.\n\n    This is useful for composition with other selectors.\n\n    See Also\n    --------\n    all : Select all columns in the current scope.\n\n    Examples\n    --------\n    >>> import polars.selectors as cs\n    >>> pl.DataFrame({\"a\": 1, \"b\": 2}).select(cs.empty())","sourceCodeStart":1277,"sourceCodeEnd":1313,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/selectors.py#L1277-L1313","documentation":"Thrown by cs.by_name (py-polars/src/polars/selectors.py:1295) when a TOP-LEVEL argument is neither a str nor a Collection of str. by_name(*names) accepts \"a\", [\"a\", \"b\"], or mixed str/collection args; an outer item that is an int, None, a dtype, etc. hits the else-branch and raises TypeError.","triggerScenarios":"cs.by_name(1), cs.by_name(None), cs.by_name(pl.Int64) (passing a dtype where a name is expected), or cs.by_name(*cols) where cols contains a non-str element.","commonSituations":"Passing an index or dtype by mistake (confusing by_name with by_index/by_dtype), or splatting an unvalidated list of identifiers that contains ints or None.","solutions":["Pass names as strings: cs.by_name(\"a\", \"b\") or a list cs.by_name([\"a\", \"b\"])","If you meant position or dtype, use the right selector: cs.by_index(0) or cs.by_dtype(pl.Int64)","Sanitize dynamic lists: cs.by_name([n for n in names if isinstance(n, str)]) or coerce with str()"],"exampleFix":"# before\nsel = cs.by_name(1)  # int is not a name\n\n# after\nsel = cs.by_name(\"1\")\n# if a position was intended:\nsel = cs.by_index(1)","handlingStrategy":"type-guard","validationCode":"def is_name_arg(x: object) -> bool:\n    return isinstance(x, str) or (isinstance(x, Collection) and all(isinstance(n, str) for n in x))\n\nassert all(is_name_arg(a) for a in args), f\"by_name args invalid: {args!r}\"","typeGuard":"from typing import TypeGuard\n\ndef is_name_or_names(x: object) -> TypeGuard[str | list[str]]:\n    return isinstance(x, str)","tryCatchPattern":"try:\n    sel = cs.by_name(*args)\nexcept TypeError as e:\n    if \"invalid name\" in str(e):\n        raise ValueError(\"by_name expects column name strings; use by_index/by_dtype for positions/dtypes\") from e\n    raise","preventionTips":["Pick the selector matching intent: by_name for strings, by_index for ints, by_dtype for dtypes","Validate splatted argument lists before selector construction"],"tags":["polars","selectors","typeerror","column-selection"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}