Skip to content

PERF: Styler.tooltips #43737

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

Merged
merged 2 commits into from
Sep 25, 2021
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
14 changes: 14 additions & 0 deletions asv_bench/benchmarks/io/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ def peakmem_classes_render(self, cols, rows):
self._style_classes()
self.st._render_html(True, True)

def time_tooltips_render(self, cols, rows):
self._style_tooltips()
self.st._render_html(True, True)

def peakmem_tooltips_render(self, cols, rows):
self._style_tooltips()
self.st._render_html(True, True)

def time_format_render(self, cols, rows):
self._style_format()
self.st._render_html(True, True)
Expand Down Expand Up @@ -77,3 +85,9 @@ def _style_apply_format_hide(self):
self.st.format("{:.3f}")
self.st.hide_index(self.st.index[1:])
self.st.hide_columns(self.st.columns[1:])

def _style_tooltips(self):
ttips = DataFrame("abc", index=self.df.index[::2], columns=self.df.columns[::2])
self.st = self.df.style.set_tooltips(ttips)
self.st.hide_index(self.st.index[12:])
self.st.hide_columns(self.st.columns[12:])
16 changes: 9 additions & 7 deletions pandas/io/formats/style_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ def _translate(
d.update({"table_attributes": table_attr})

if self.tooltips:
d = self.tooltips._translate(self.data, self.uuid, d)
d = self.tooltips._translate(self, d)

return d

Expand Down Expand Up @@ -1508,7 +1508,7 @@ def _pseudo_css(self, uuid: str, name: str, row: int, col: int, text: str):
},
]

def _translate(self, styler_data: DataFrame | Series, uuid: str, d: dict):
def _translate(self, styler: StylerRenderer, d: dict):
"""
Mutate the render dictionary to allow for tooltips:

Expand All @@ -1529,21 +1529,23 @@ def _translate(self, styler_data: DataFrame | Series, uuid: str, d: dict):
-------
render_dict : Dict
"""
self.tt_data = self.tt_data.reindex_like(styler_data)

self.tt_data = self.tt_data.reindex_like(styler.data)
if self.tt_data.empty:
return d

name = self.class_name

mask = (self.tt_data.isna()) | (self.tt_data.eq("")) # empty string = no ttip
self.table_styles = [
style
for sublist in [
self._pseudo_css(uuid, name, i, j, str(self.tt_data.iloc[i, j]))
self._pseudo_css(styler.uuid, name, i, j, str(self.tt_data.iloc[i, j]))
for i in range(len(self.tt_data.index))
for j in range(len(self.tt_data.columns))
if not mask.iloc[i, j]
if not (
mask.iloc[i, j]
or i in styler.hidden_rows
or j in styler.hidden_columns
)
]
for style in sublist
]
Expand Down