Skip to content

Commit 8a61d30

Browse files
authored
PERF: Styler.tooltips (#43737)
1 parent 004a1c9 commit 8a61d30

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

asv_bench/benchmarks/io/style.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ def peakmem_classes_render(self, cols, rows):
3434
self._style_classes()
3535
self.st._render_html(True, True)
3636

37+
def time_tooltips_render(self, cols, rows):
38+
self._style_tooltips()
39+
self.st._render_html(True, True)
40+
41+
def peakmem_tooltips_render(self, cols, rows):
42+
self._style_tooltips()
43+
self.st._render_html(True, True)
44+
3745
def time_format_render(self, cols, rows):
3846
self._style_format()
3947
self.st._render_html(True, True)
@@ -77,3 +85,9 @@ def _style_apply_format_hide(self):
7785
self.st.format("{:.3f}")
7886
self.st.hide_index(self.st.index[1:])
7987
self.st.hide_columns(self.st.columns[1:])
88+
89+
def _style_tooltips(self):
90+
ttips = DataFrame("abc", index=self.df.index[::2], columns=self.df.columns[::2])
91+
self.st = self.df.style.set_tooltips(ttips)
92+
self.st.hide_index(self.st.index[12:])
93+
self.st.hide_columns(self.st.columns[12:])

pandas/io/formats/style_render.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ def _translate(
294294
d.update({"table_attributes": table_attr})
295295

296296
if self.tooltips:
297-
d = self.tooltips._translate(self.data, self.uuid, d)
297+
d = self.tooltips._translate(self, d)
298298

299299
return d
300300

@@ -1508,7 +1508,7 @@ def _pseudo_css(self, uuid: str, name: str, row: int, col: int, text: str):
15081508
},
15091509
]
15101510

1511-
def _translate(self, styler_data: DataFrame | Series, uuid: str, d: dict):
1511+
def _translate(self, styler: StylerRenderer, d: dict):
15121512
"""
15131513
Mutate the render dictionary to allow for tooltips:
15141514
@@ -1529,21 +1529,23 @@ def _translate(self, styler_data: DataFrame | Series, uuid: str, d: dict):
15291529
-------
15301530
render_dict : Dict
15311531
"""
1532-
self.tt_data = self.tt_data.reindex_like(styler_data)
1533-
1532+
self.tt_data = self.tt_data.reindex_like(styler.data)
15341533
if self.tt_data.empty:
15351534
return d
15361535

15371536
name = self.class_name
1538-
15391537
mask = (self.tt_data.isna()) | (self.tt_data.eq("")) # empty string = no ttip
15401538
self.table_styles = [
15411539
style
15421540
for sublist in [
1543-
self._pseudo_css(uuid, name, i, j, str(self.tt_data.iloc[i, j]))
1541+
self._pseudo_css(styler.uuid, name, i, j, str(self.tt_data.iloc[i, j]))
15441542
for i in range(len(self.tt_data.index))
15451543
for j in range(len(self.tt_data.columns))
1546-
if not mask.iloc[i, j]
1544+
if not (
1545+
mask.iloc[i, j]
1546+
or i in styler.hidden_rows
1547+
or j in styler.hidden_columns
1548+
)
15471549
]
15481550
for style in sublist
15491551
]

0 commit comments

Comments
 (0)