pola-rs/polars · error · ValueError

cannot call `plot.line` when Series name is 'index'

Error message

cannot call `plot.line` when Series name is 'index'

What it means

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.

Solutions

  1. Rename the Series: s.rename("value").plot.line()
  2. Use s.alias("y_value") before plotting
  3. If you need the original index semantics, keep it in a DataFrame and use DataFrame.plot instead

Example fix

// before
s.rename("index").plot.line()
// after
s.rename("position").plot.line()
Defensive patterns

Strategy: validation

Validate before calling

if s.name == "index":
    s = s.rename("value")
s.plot.line()

Try / catch

try:
    chart = s.plot.line()
except ValueError:
    chart = s.rename("value").plot.line()

Prevention

When it happens

Trigger: 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'.

Common situations: 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'.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/1e48360cb9e3dff8. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/series/plotting.py:158

        .. versionchanged:: 1.6.0
            In prior versions of Polars, HvPlot was the plotting backend. If you would
            like to restore the previous plotting functionality, all you need to do
            is add `import hvplot.polars` at the top of your script and replace
            `df.plot` with `df.hvplot`.

        Parameters
        ----------
        **kwargs
            Additional keyword arguments passed to Altair.

        Examples
        --------
        >>> s = pl.Series("price", [1, 3, 3, 3, 5, 2, 6, 5, 5, 5, 7])
        >>> s.plot.line()  # doctest: +SKIP
        """  # noqa: W505
        if self._series_name == "index":
            msg = "cannot call `plot.line` when Series name is 'index'"
            raise ValueError(msg)
        encodings: Encodings = {"x": "index", "y": self._series_name}
        return (
            alt.Chart(self._df.with_row_index())
            .mark_line(tooltip=True)
            .encode(**encodings, **kwargs)
            .interactive()
        )

    def __getattr__(self, attr: str) -> Callable[..., alt.Chart]:
        if self._series_name == "index":
            msg = f"Cannot call `plot.{attr}` when Series name is 'index'"
            raise ValueError(msg)
        if attr == "scatter":
            # alias `scatter` to `point` because of how common it is
            attr = "point"
        method = getattr(alt.Chart(self._df.with_row_index()), f"mark_{attr}", None)
        if method is None:
            msg = f"Altair has no method 'mark_{attr}'"

View on GitHub (pinned to fe841f959e)