Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Altair tooltip was being incorrectly applied to plots which did not accept it #19789

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"]