{"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":"ValueError","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/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/series/plotting.py#L152-L188","documentation":"SeriesPlot.__getattr__ dispatches unknown attributes to Altair mark_* methods. Before doing so it rejects ANY plot method when the Series is named 'index', because the generated x-encoding field 'index' (from with_row_index()) would collide with the series name — raising ValueError.","triggerScenarios":"Calling any non-predefined plot attribute on a Series named 'index', e.g. s.rename(\"index\").plot.scatter(), s.plot.area(), s.plot.bar().","commonSituations":"Same as plot.line: pandas-migrated frames with an 'index' column; hitting this via dynamic attribute access or when using mark aliases like 'scatter'.","solutions":["Rename the Series before plotting: s.rename(\"value\").plot.scatter()","Use s.alias(...) to change the name in an expression chain","Build the Altair chart manually if you need 'index' as the name"],"exampleFix":"// before\ns.rename(\"index\").plot.scatter()\n// after\ns.rename(\"x_val\").plot.scatter()  # 'scatter' aliases to mark_point","handlingStrategy":"validation","validationCode":"if s.name == \"index\":\n    s = s.rename(\"value\")\ngetattr(s.plot, attr)()","typeGuard":null,"tryCatchPattern":"try:\n    chart = getattr(s.plot, attr)()\nexcept ValueError:\n    chart = getattr(s.rename(\"value\").plot, attr)()","preventionTips":["Never keep Series named 'index' when using any plot method, not just line","Rename before dynamic plot attribute dispatch","Centralize plotting behind a helper that checks/reserves the name"],"tags":["polars","plotting","altair","value-error","reserved-name"],"backgroundTag":"invalid-argument-value","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"}