{"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":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/series/series.py","lineNumber":323,"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":305,"sourceCodeEnd":341,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/series/series.py#L305-L341","documentation":"The Series constructor requires the `name` argument to be a string (or omitted). If you pass two positional arguments and the first is not a str, polars raises this TypeError instead of silently coercing.","triggerScenarios":"pl.Series(123, [1,2,3]) — passing a non-string first argument while also supplying values; pl.Series(some_series, values) passing a Series/DataFrame as the name; swapped-argument mistakes.","commonSituations":"Intending the two-argument shortcut pl.Series(name, values) but passing a non-str name (e.g. a column index or an existing Series object); copying code where the first arg was a dtype hint.","solutions":["Pass a string name: pl.Series(\"values\", [1,2,3])","Omit the name entirely: pl.Series([1,2,3])","If you meant to pass data only, make the first positional arg the values or use the values keyword","Convert the intended name with str(...) if it's a scalar like an int"],"exampleFix":"// before\npl.Series(0, [1, 2, 3])\n// after\npl.Series(\"values\", [1, 2, 3])  # or pl.Series([1, 2, 3])","handlingStrategy":"type-guard","validationCode":"if not isinstance(name, str):\n    raise TypeError(f\"name must be str, got {type(name).__name__}\")\npl.Series(name, values)","typeGuard":"def is_str_or_none(x) -> bool:\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)","preventionTips":["Always pass a str as the first positional arg, or omit it entirely","Don't pass a Series/DataFrame as the name — extract .name first","Use keyword form pl.Series(values=..., name=...) to avoid swap mistakes"],"tags":["polars","series","constructor","type-error"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}