1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
import numpy as np
from pandas import (
DataFrame,
IndexSlice,
)
class Render:
params = [[12, 24, 36], [12, 120]]
param_names = ["cols", "rows"]
def setup(self, cols, rows):
self.df = DataFrame(
np.random.randn(rows, cols),
columns=[f"float_{i+1}" for i in range(cols)],
index=[f"row_{i+1}" for i in range(rows)],
)
def time_apply_render(self, cols, rows):
self._style_apply()
self.st._render_html(True, True)
def peakmem_apply_render(self, cols, rows):
self._style_apply()
self.st._render_html(True, True)
def time_classes_render(self, cols, rows):
self._style_classes()
self.st._render_html(True, True)
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)
def peakmem_format_render(self, cols, rows):
self._style_format()
self.st._render_html(True, True)
def time_apply_format_hide_render(self, cols, rows):
self._style_apply_format_hide()
self.st._render_html(True, True)
def peakmem_apply_format_hide_render(self, cols, rows):
self._style_apply_format_hide()
self.st._render_html(True, True)
def _style_apply(self):
def _apply_func(s):
return [
"background-color: lightcyan" if s.name == "row_1" else "" for v in s
]
self.st = self.df.style.apply(_apply_func, axis=1)
def _style_classes(self):
classes = self.df.map(lambda v: ("cls-1" if v > 0 else ""))
classes.index, classes.columns = self.df.index, self.df.columns
self.st = self.df.style.set_td_classes(classes)
def _style_format(self):
ic = int(len(self.df.columns) / 4 * 3)
ir = int(len(self.df.index) / 4 * 3)
# apply a formatting function
# subset is flexible but hinders vectorised solutions
self.st = self.df.style.format(
"{:,.3f}", subset=IndexSlice["row_1":f"row_{ir}", "float_1":f"float_{ic}"]
)
def _style_apply_format_hide(self):
self.st = self.df.style.map(lambda v: "color: red;")
self.st.format("{:.3f}")
self.st.hide(self.st.index[1:], axis=0)
self.st.hide(self.st.columns[1:], axis=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(self.st.index[12:], axis=0)
self.st.hide(self.st.columns[12:], axis=1)
|