{"record":{"id":"57b582a59334f5d7","repo":"pola-rs/polars","slug":"cannot-use-plot-hist-when-series-name-is-count","errorCode":null,"errorMessage":"cannot use `plot.hist` when Series name is `'count()'`","messagePattern":"cannot use `plot\\.hist` when Series name is `'count\\(\\)'`","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/series/plotting.py","lineNumber":68,"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 arguments and 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.hist()  # doctest: +SKIP\n        \"\"\"  # noqa: W505\n        if self._series_name == \"count()\":\n            msg = \"cannot use `plot.hist` when Series name is `'count()'`\"\n            raise ValueError(msg)\n        encodings: Encodings = {\n            \"x\": alt.X(f\"{self._series_name}:Q\", bin=True),\n            \"y\": \"count()\",\n        }\n        return (\n            alt.Chart(self._df)\n            .mark_bar(tooltip=True)\n            .encode(**encodings, **kwargs)\n            .interactive()\n        )\n\n    def kde(\n        self,\n        /,\n        **kwargs: Unpack[EncodeKwds],\n    ) -> alt.Chart:\n        \"\"\"\n        Draw kernel density estimate plot.","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/series/plotting.py#L50-L86","documentation":"Series.plot.hist() builds an Altair chart that encodes the y-axis as the literal Vega-Lite aggregate 'count()'. If the Series itself is named 'count()', the generated encoding collides (duplicate/conflicting field name), so polars raises ValueError upfront.","triggerScenarios":"Calling s.plot.hist() on a Series whose name is exactly 'count()' — typically a Series produced by a group_by().len()/count() aggregation that kept the default 'count' name variant or was explicitly named 'count()'.","commonSituations":"Chaining plotting directly onto a counting aggregation like df.group_by(\"x\").len().get_column(\"len\") after renaming to 'count()'; round-tripping through systems that emit Vega-Lite-style aggregate names.","solutions":["Rename the Series first: s.rename(\"value\").plot.hist()","Use s.alias(\"frequency\") before plotting","Set the name at construction: pl.Series(\"price\", values).plot.hist()"],"exampleFix":"// before\ns.rename(\"count()\").plot.hist()\n// after\ns.rename(\"frequency\").plot.hist()","handlingStrategy":"validation","validationCode":"if s.name == \"count()\":\n    s = s.rename(\"frequency\")\ns.plot.hist()","typeGuard":null,"tryCatchPattern":"try:\n    chart = s.plot.hist()\nexcept ValueError:\n    chart = s.rename(\"frequency\").plot.hist()","preventionTips":["Avoid naming Series after Vega-Lite aggregate names like 'count()'","Rename aggregation outputs immediately after group_by().len()/count()","Add an assertion on s.name before plotting 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"}