{"record":{"id":"334030ec14675f5b","repo":"pola-rs/polars","slug":"cannot-call-plot-attr-when-series-name-is-ind","errorCode":null,"errorMessage":"Cannot call `plot.{attr}` when Series name is 'index'","messagePattern":"Cannot call `plot\\.(.+?)` when Series name is 'index'","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/series/plotting.py","lineNumber":170,"sourceCode":"        --------\n        >>> s = pl.Series(\"price\", [1, 3, 3, 3, 5, 2, 6, 5, 5, 5, 7])\n        >>> s.plot.line()  # doctest: +SKIP\n        \"\"\"  # noqa: W505\n        if self._series_name == \"index\":\n            msg = \"cannot call `plot.line` when Series name is 'index'\"\n            raise ValueError(msg)\n        encodings: Encodings = {\"x\": \"index\", \"y\": self._series_name}\n        return (\n            alt.Chart(self._df.with_row_index())\n            .mark_line(tooltip=True)\n            .encode(**encodings, **kwargs)\n            .interactive()\n        )\n\n    def __getattr__(self, attr: str) -> Callable[..., alt.Chart]:\n        if self._series_name == \"index\":\n            msg = f\"Cannot call `plot.{attr}` when Series name is 'index'\"\n            raise ValueError(msg)\n        if attr == \"scatter\":\n            # alias `scatter` to `point` because of how common it is\n            attr = \"point\"\n        method = getattr(alt.Chart(self._df.with_row_index()), f\"mark_{attr}\", None)\n        if method is None:\n            msg = f\"Altair has no method 'mark_{attr}'\"\n            raise AttributeError(msg)\n        encodings: Encodings = {\"x\": \"index\", \"y\": self._series_name}\n\n        accepts_tooltip_argument = \"tooltip\" in {\n            value.name for value in inspect.signature(method).parameters.values()\n        }\n        if accepts_tooltip_argument:\n\n            def func(**kwargs: EncodeKwds) -> alt.Chart:\n                return method(tooltip=True).encode(**encodings, **kwargs).interactive()\n        else:\n","sourceCodeStart":152,"sourceCodeEnd":188,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/series/plotting.py#L152-L188","documentation":"Raised by SeriesPlot.__getattr__ (py-polars/src/polars/series/plotting.py:170) for ANY dynamically-dispatched chart method (s.plot.bar, s.plot.point, s.plot.area, ...) when the Series is named 'index'. Like plot.line, these charts synthesize the x-axis via with_row_index() (column 'index'), so a Series named 'index' collides with the x encoding. Only plot.hist, plot.kde and plot.line have their own messages; everything else goes through this generic one.","triggerScenarios":"pl.Series(\"index\", [...]).plot.bar(), df[\"index\"].plot.point(), df[\"index\"].plot.scatter() (scatter is aliased to point first, then still fails the name check).","commonSituations":"Same as the line variant: pandas-style 'index' column names, plotting row-number series. Developers hit this via any mark_* method, so the message names plot.<attr> with the actual attribute used.","solutions":["Rename the Series before plotting: s.rename(\"row\").plot.bar()","Rename the column upstream: df = df.rename({\"index\": \"row_num\"})","Treat 'index' as a reserved name for charting code paths; add a rename step in your plotting helper"],"exampleFix":"# before\npl.Series(\"index\", [1, 2, 3]).plot.bar()  # ValueError\n\n# after\npl.Series(\"index\", [1, 2, 3]).rename(\"row\").plot.bar()","handlingStrategy":"validation","validationCode":"def plot_series(s, mark=\"bar\", **kw):\n    if s.name == \"index\":\n        s = s.rename(\"row\")\n    return getattr(s.plot, mark)(**kw)","typeGuard":null,"tryCatchPattern":"try:\n    chart = s.plot.point()\nexcept ValueError as e:\n    if \"Series name is 'index'\" in str(e):\n        chart = s.rename(\"row\").plot.point()\n    else:\n        raise","preventionTips":["Centralize Series plotting in one helper that renames 'index' first","Ban 'index' as a column name in schemas for projects that chart with polars/Altair"],"tags":["polars","plotting","altair","valueerror","naming"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}