Skip to content

Commit

Permalink
fix: Altair tooltip was being incorrectly applied to plots which did …
Browse files Browse the repository at this point in the history
…not accept it (#19789)
  • Loading branch information
MarcoGorelli authored Nov 15, 2024
1 parent c7e9126 commit f45c9e9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
15 changes: 12 additions & 3 deletions py-polars/polars/dataframe/plotting.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import inspect
from typing import TYPE_CHECKING, Callable, Union

from polars.dependencies import altair as alt
Expand Down Expand Up @@ -239,9 +240,17 @@ def __getattr__(self, attr: str) -> Callable[..., alt.Chart]:
if method is None:
msg = "Altair has no method 'mark_{attr}'"
raise AttributeError(msg)
encodings: Encodings = {}

def func(**kwargs: EncodeKwds) -> alt.Chart:
return method(tooltip=True).encode(**encodings, **kwargs).interactive()
accepts_tooltip_argument = "tooltip" in {
value.name for value in inspect.signature(method).parameters.values()
}
if accepts_tooltip_argument:

def func(**kwargs: EncodeKwds) -> alt.Chart:
return method(tooltip=True).encode(**kwargs).interactive()
else:

def func(**kwargs: EncodeKwds) -> alt.Chart:
return method().encode(**kwargs).interactive()

return func
14 changes: 12 additions & 2 deletions py-polars/polars/series/plotting.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import inspect
from typing import TYPE_CHECKING, Callable

from polars.dependencies import altair as alt
Expand Down Expand Up @@ -175,7 +176,16 @@ def __getattr__(self, attr: str) -> Callable[..., alt.Chart]:
raise AttributeError(msg)
encodings: Encodings = {"x": "index", "y": self._series_name}

def func(**kwargs: EncodeKwds) -> alt.Chart:
return method(tooltip=True).encode(**encodings, **kwargs).interactive()
accepts_tooltip_argument = "tooltip" in {
value.name for value in inspect.signature(method).parameters.values()
}
if accepts_tooltip_argument:

def func(**kwargs: EncodeKwds) -> alt.Chart:
return method(tooltip=True).encode(**encodings, **kwargs).interactive()
else:

def func(**kwargs: EncodeKwds) -> alt.Chart:
return method().encode(**encodings, **kwargs).interactive()

return func
8 changes: 8 additions & 0 deletions py-polars/tests/unit/operations/namespaces/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,11 @@ def test_x_with_axis_18830() -> None:
df = pl.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]})
result = df.plot.line(x=alt.X("a", axis=alt.Axis(labelAngle=-90))).to_dict()
assert result["mark"]["tooltip"] is True


def test_errorbar_19787() -> None:
df = pl.DataFrame({"A": [0, 1, 2], "B": [10, 11, 12], "C": [1, 2, 3]})
result = df.plot.errorbar(x="A", y="B", yError="C").to_dict()
assert "tooltip" not in result["encoding"]
result = df["A"].plot.errorbar().to_dict()
assert "tooltip" not in result["encoding"]

0 comments on commit f45c9e9

Please sign in to comment.