{"record":{"id":"db24bec542ef8b25","repo":"pola-rs/polars","slug":"series-name-must-be-a-string","errorCode":null,"errorMessage":"Series name must be a string","messagePattern":"Series name must be a string","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/series/series.py","lineNumber":293,"sourceCode":"        # If 'Unknown' treat as None to trigger type inference\n        if dtype == Unknown:\n            dtype = None\n        elif dtype is not None and not is_polars_dtype(dtype):\n            dtype = parse_into_dtype(dtype)\n\n        # Handle case where values are passed as the first argument\n        original_name: str | None = None\n        if name is None:\n            name = \"\"\n        elif isinstance(name, str):\n            original_name = name\n        else:\n            if values is None:\n                values = name\n                name = \"\"\n            else:\n                msg = \"Series name must be a string\"\n                raise TypeError(msg)\n\n        if isinstance(values, Sequence):\n            self._s = sequence_to_pyseries(\n                name,\n                values,\n                dtype=dtype,\n                strict=strict,\n                nan_to_null=nan_to_null,\n            )\n\n        elif values is None:\n            self._s = sequence_to_pyseries(name, [], dtype=dtype)\n\n        elif _check_for_numpy(values) and isinstance(values, np.ndarray):\n            self._s = numpy_to_pyseries(\n                name, values, strict=strict, nan_to_null=nan_to_null\n            )\n            if values.dtype.type in [np.datetime64, np.timedelta64]:","sourceCodeStart":275,"sourceCodeEnd":311,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/series/series.py#L275-L311","documentation":"Raised by the Series constructor (py-polars/src/polars/series/series.py:293) when name is given, is not a string, AND values are also given. The constructor has one convenience overload — pl.Series([1,2,3]) puts the data in the name slot — but that only works when values is None; once both positional args are supplied, a non-str name is unambiguous misuse and raises TypeError.","triggerScenarios":"pl.Series(0, [1, 2, 3]) (index-like name), pl.Series((\"a\",), [1, 2]), pl.Series(1.0, [1.0]), or splatting a variable that is sometimes a list: pl.Series(maybe_data, other_data).","commonSituations":"Pandas habits (pd.Series(data, index=...)) where a number is passed as the first arg; code that passes a name variable which is None only sometimes — note pl.Series(None, [1,2]) is fine because None is special-cased; passing a tuple/dict as name.","solutions":["Give the name as a string: pl.Series(\"prices\", [1, 2, 3])","If the first arg was meant to be data only, drop the second: pl.Series([1, 2, 3])","If name comes from a variable, coerce: pl.Series(str(name) if name is not None else None, values)"],"exampleFix":"# before\ns = pl.Series(0, [10, 20, 30])  # int name + values\n\n# after\ns = pl.Series(\"prices\", [10, 20, 30])\n# data-only form:\ns = pl.Series([10, 20, 30])","handlingStrategy":"type-guard","validationCode":"def make_series(name, values):\n    if name is not None and not isinstance(name, str):\n        raise TypeError(f\"Series name must be str, got {type(name).__name__}\")\n    return pl.Series(name, values)","typeGuard":"from typing import TypeGuard\n\ndef is_series_name(x: object) -> TypeGuard[str]:\n    return x is None or isinstance(x, str)","tryCatchPattern":"try:\n    s = pl.Series(name, values)\nexcept TypeError as e:\n    if \"name must be a string\" in str(e):\n        s = pl.Series(str(name), values)\n    else:\n        raise","preventionTips":["Always pass the name as a str (or omit it); pl.Series(None, values) also works","Static type checkers catch this: annotate name: str | None at call sites"],"tags":["polars","series","typeerror","constructor","api-misuse"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}