From aa67dbe7b0e744b4412e11e70a63f87f33ae82c7 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sat, 25 Sep 2021 12:30:28 +0200 Subject: [PATCH 1/2] exclude hidden content --- asv_bench/benchmarks/io/style.py | 14 ++++++++++++++ pandas/io/formats/style_render.py | 16 +++++++++------- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/asv_bench/benchmarks/io/style.py b/asv_bench/benchmarks/io/style.py index d3934942b7dc4..eb23e9aa69dd2 100644 --- a/asv_bench/benchmarks/io/style.py +++ b/asv_bench/benchmarks/io/style.py @@ -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) @@ -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("text", index=self.df.index[::2], columns=self.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:]) diff --git a/pandas/io/formats/style_render.py b/pandas/io/formats/style_render.py index b0e729bb926bf..cfe5b2c2bdfab 100644 --- a/pandas/io/formats/style_render.py +++ b/pandas/io/formats/style_render.py @@ -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 @@ -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: @@ -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 ] From 7766f05564945533c6c6dd64d2912d48a5712b49 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sat, 25 Sep 2021 12:41:11 +0200 Subject: [PATCH 2/2] exclude hidden content --- asv_bench/benchmarks/io/style.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/asv_bench/benchmarks/io/style.py b/asv_bench/benchmarks/io/style.py index eb23e9aa69dd2..f0902c9c2c328 100644 --- a/asv_bench/benchmarks/io/style.py +++ b/asv_bench/benchmarks/io/style.py @@ -87,7 +87,7 @@ def _style_apply_format_hide(self): self.st.hide_columns(self.st.columns[1:]) def _style_tooltips(self): - ttips = DataFrame("text", index=self.df.index[::2], columns=self.columns[::2]) + 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:])