pola-rs/polars · error · ValueError
cannot use `plot.kde` when Series name is `'density'`
Error message
cannot use `plot.kde` when Series name is `'density'`
What it means
Series.plot.kde() encodes the y-axis with the Vega-Lite transform output field 'density' produced by transform_density. If the Series is named 'density', the source field and the generated density field collide, so polars raises ValueError upfront.
Solutions
- Rename the Series: s.rename("value").plot.kde()
- Use s.alias("measurement") before plotting
- Pass a differently named Series at construction time
Example fix
// before
s.alias("density").plot.kde()
// after
s.alias("density_estimate").plot.kde() Defensive patterns
Strategy: validation
Validate before calling
if s.name == "density":
s = s.rename("value")
s.plot.kde() Try / catch
try:
chart = s.plot.kde()
except ValueError:
chart = s.rename("value").plot.kde() Prevention
- Don't name Series 'density' if you plan to plot KDE
- Rename outputs of prior density computations before plotting
- Standardize plot-friendly names ('value', 'observation') in pipelines
When it happens
Trigger: Calling s.plot.kde() on a Series named exactly 'density' — often a Series that came from a previous density computation or was renamed to 'density'.
Common situations: Plotting KDE of a column produced by another KDE/density transform; data pipelines that normalize column names to statistical terms like 'density'.
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
- Cannot call `plot. ` when Series name is 'index
- cannot call `plot.line` when Series name is 'index'
- cannot use `plot.hist` when Series name is `'count()'`
- altair>=5.4.0 is required for `.plot`
- Altair has no method 'mark_
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/9b5cd8517ac6d913.
Report an issue: GitHub.
Appendix: source
Thrown at py-polars/src/polars/series/plotting.py:114
.. 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.kde() # doctest: +SKIP
""" # noqa: W505
if self._series_name == "density":
msg = "cannot use `plot.kde` when Series name is `'density'`"
raise ValueError(msg)
encodings: Encodings = {"x": self._series_name, "y": "density:Q"}
return (
alt.Chart(self._df)
.transform_density(self._series_name, as_=[self._series_name, "density"])
.mark_area(tooltip=True)
.encode(**encodings, **kwargs)
.interactive()
)
def line(
self,
/,
**kwargs: Unpack[EncodeKwds],
) -> alt.Chart:
"""
Draw line plot.
Polars does not implement plotting logic itself but instead defers toView on GitHub (pinned to fe841f959e)