{"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":null,"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/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/series/plotting.py#L140-L176","documentation":"Raised by Series.plot.line (py-polars/src/polars/series/plotting.py:158) when the Series is named 'index'. The line plot uses with_row_index() to synthesize an x-axis column named 'index'; a Series already named 'index' would make x and y reference the same field, so polars refuses with ValueError.","triggerScenarios":"pl.Series(\"index\", [...]).plot.line(), or df[\"index\"].plot.line() on any DataFrame column literally named 'index' (e.g. after reset_index-style operations ported from pandas, or CSV/database columns named 'index').","commonSituations":"Migrating pandas workflows where 'index' is a common column name; plotting a row-number column; data from sources that use 'index' as a field. Affects plot.line and every mark_* chart (see the __getattr__ variant, error 510).","solutions":["Rename before plotting: s.rename(\"idx\").plot.line()","Rename the column in the DataFrame: df = df.rename({\"index\": \"row\"}) then df[\"row\"].plot.line()","If you want the index on the x-axis of a differently named column, that already works: df[\"value\"].plot.line()"],"exampleFix":"# before\ndf[\"index\"].plot.line()  # ValueError\n\n# after\ndf[\"index\"].rename(\"row\").plot.line()","handlingStrategy":"validation","validationCode":"s_safe = s.rename(\"row\") if s.name == \"index\" else s\ns_safe.plot.line()","typeGuard":null,"tryCatchPattern":"try:\n    chart = s.plot.line()\nexcept ValueError as e:\n    if \"'index'\" in str(e):\n        chart = s.rename(\"row\").plot.line()\n    else:\n        raise","preventionTips":["Treat 'index' as reserved in code that plots Series; rename pandas-migrated 'index' columns on ingest","Prefer plotting the value column directly — the row index is already the default x axis"],"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"}