{"record":{"id":"1e48360cb9e3dff8","repo":"pola-rs/polars","slug":"cannot-call-plot-line-when-series-name-is-index","errorCode":null,"errorMessage":"cannot call `plot.line` when Series name is 'index'","messagePattern":"cannot call `plot\\.line` when Series name is 'index'","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/series/plotting.py","lineNumber":158,"sourceCode":"        .. versionchanged:: 1.6.0\n            In prior versions of Polars, HvPlot was the plotting backend. If you would\n            like to restore the previous plotting functionality, all you need to do\n            is add `import hvplot.polars` at the top of your script and replace\n            `df.plot` with `df.hvplot`.\n\n        Parameters\n        ----------\n        **kwargs\n            Additional keyword arguments passed to Altair.\n\n        Examples\n        --------\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}'\"","sourceCodeStart":140,"sourceCodeEnd":176,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/series/plotting.py#L140-L176","documentation":"Series.plot.line() adds a row index and encodes the x-axis as the literal field name 'index' (from with_row_index()). If the Series itself is named 'index', the two encodings collide, so polars raises ValueError.","triggerScenarios":"Calling s.plot.line() on a Series named exactly 'index' — e.g. after df.to_series() on an index column or a Series renamed to 'index'.","commonSituations":"Pandas-to-polars migrations where 'index' is a common column name; plotting a Series extracted from a DataFrame that had a column literally called 'index'.","solutions":["Rename the Series: s.rename(\"value\").plot.line()","Use s.alias(\"y_value\") before plotting","If you need the original index semantics, keep it in a DataFrame and use DataFrame.plot instead"],"exampleFix":"// before\ns.rename(\"index\").plot.line()\n// after\ns.rename(\"position\").plot.line()","handlingStrategy":"validation","validationCode":"if s.name == \"index\":\n    s = s.rename(\"value\")\ns.plot.line()","typeGuard":null,"tryCatchPattern":"try:\n    chart = s.plot.line()\nexcept ValueError:\n    chart = s.rename(\"value\").plot.line()","preventionTips":["Avoid 'index' as a Series/column name when using polars plotting","Rename after pandas migrations instead of keeping 'index' literally","Note the reserved name affects all plot.* methods via __getattr__"],"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"}