{"record":{"id":"9b5cd8517ac6d913","repo":"pola-rs/polars","slug":"cannot-use-plot-kde-when-series-name-is-densit","errorCode":null,"errorMessage":"cannot use `plot.kde` when Series name is `'density'`","messagePattern":"cannot use `plot\\.kde` when Series name is `'density'`","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/series/plotting.py","lineNumber":114,"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.kde()  # doctest: +SKIP\n        \"\"\"  # noqa: W505\n        if self._series_name == \"density\":\n            msg = \"cannot use `plot.kde` when Series name is `'density'`\"\n            raise ValueError(msg)\n        encodings: Encodings = {\"x\": self._series_name, \"y\": \"density:Q\"}\n        return (\n            alt.Chart(self._df)\n            .transform_density(self._series_name, as_=[self._series_name, \"density\"])\n            .mark_area(tooltip=True)\n            .encode(**encodings, **kwargs)\n            .interactive()\n        )\n\n    def line(\n        self,\n        /,\n        **kwargs: Unpack[EncodeKwds],\n    ) -> alt.Chart:\n        \"\"\"\n        Draw line plot.\n\n        Polars does not implement plotting logic itself but instead defers to","sourceCodeStart":96,"sourceCodeEnd":132,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/series/plotting.py#L96-L132","documentation":"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.","triggerScenarios":"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'.","commonSituations":"Plotting KDE of a column produced by another KDE/density transform; data pipelines that normalize column names to statistical terms like 'density'.","solutions":["Rename the Series: s.rename(\"value\").plot.kde()","Use s.alias(\"measurement\") before plotting","Pass a differently named Series at construction time"],"exampleFix":"// before\ns.alias(\"density\").plot.kde()\n// after\ns.alias(\"density_estimate\").plot.kde()","handlingStrategy":"validation","validationCode":"if s.name == \"density\":\n    s = s.rename(\"value\")\ns.plot.kde()","typeGuard":null,"tryCatchPattern":"try:\n    chart = s.plot.kde()\nexcept ValueError:\n    chart = s.rename(\"value\").plot.kde()","preventionTips":["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"],"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"}