{"record":{"id":"5200f7f6cb3b7411","repo":"pola-rs/polars","slug":"invalid-name-n-r","errorCode":null,"errorMessage":"invalid name: {n!r}","messagePattern":"invalid name: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/selectors.py","lineNumber":1291,"sourceCode":"    shape: (2, 2)\n    ┌─────┬───────┐\n    │ baz ┆ zap   │\n    │ --- ┆ ---   │\n    │ 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","sourceCodeStart":1273,"sourceCodeEnd":1309,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/selectors.py#L1273-L1309","documentation":"Thrown inside cs.by_name (py-polars/src/polars/selectors.py:1291) when an ELEMENT inside a collection argument is not a string. by_name accepts str names and collections of str; a non-str element (int, None, float) inside a list/tuple hits this inner check and raises TypeError with the offending item's repr.","triggerScenarios":"cs.by_name([\"a\", 1]), cs.by_name([\"a\", None]), cs.by_name([\"a\", 1.0]). The collection itself is valid, but one member is not a str.","commonSituations":"Name lists built dynamically from mixed data — e.g. DataFrame column names that were renamed to ints, None placeholders for optional columns, or values from JSON that are numbers instead of strings.","solutions":["Coerce all names to str before calling: cs.by_name([str(n) for n in names])","Filter out non-names: [n for n in names if isinstance(n, str)] (note this silently drops columns — prefer explicit str() conversion if the values are the real names)","Check the repr in the error to find the offending element and fix it at the source"],"exampleFix":"# before\nsel = cs.by_name([\"a\", 1])\n\n# after\nsel = cs.by_name([\"a\", \"1\"])  # or\nsel = cs.by_name([str(n) for n in [\"a\", 1]])","handlingStrategy":"validation","validationCode":"names = [str(n) for n in candidate_names if n is not None]\nsel = cs.by_name(names)","typeGuard":"from typing import TypeGuard\n\ndef all_str_names(items: list[object]) -> TypeGuard[list[str]]:\n    return all(isinstance(n, str) for n in items)","tryCatchPattern":"try:\n    sel = cs.by_name(names)\nexcept TypeError as e:\n    if \"invalid name\" in str(e):\n        sel = cs.by_name([str(n) for n in names])\n    else:\n        raise","preventionTips":["Coerce column identifiers to str when they come from mixed data sources","Drop None placeholders for optional columns before selecting"],"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"}