Skip to content

PERF: to speed up rendering of styler #34863

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
Jul 9, 2020
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
1 change: 1 addition & 0 deletions asv_bench/asv.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"xlwt": [],
"odfpy": [],
"pytest": [],
"jinja2": [],
// If using Windows with python 2.7 and want to build using the
// mingw toolchain (rather than MSVC), uncomment the following line.
// "libpython": [],
Expand Down
34 changes: 34 additions & 0 deletions asv_bench/benchmarks/io/style.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import numpy as np

from pandas import DataFrame


class RenderApply:

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)],
)
self._style_apply()

def time_render(self, cols, rows):
self.st.render()

def peakmem_apply(self, cols, rows):
self._style_apply()

def peakmem_render(self, cols, rows):
self.st.render()

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)
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,7 @@ Performance improvements
- Performance improvement in :class:`pandas.core.groupby.RollingGroupby` (:issue:`34052`)
- Performance improvement in arithmetic operations (sub, add, mul, div) for MultiIndex (:issue:`34297`)
- Performance improvement in `DataFrame[bool_indexer]` when `bool_indexer` is a list (:issue:`33924`)
- Significant performance improvement of :meth:`io.formats.style.Styler.render` with styles added with various ways such as :meth:`io.formats.style.Styler.apply`, :meth:`io.formats.style.Styler.applymap` or :meth:`io.formats.style.Styler.bar` (:issue:`19917`)

.. ---------------------------------------------------------------------------

Expand Down
18 changes: 13 additions & 5 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,11 +561,19 @@ def _update_ctx(self, attrs: DataFrame) -> None:
Whitespace shouldn't matter and the final trailing ';' shouldn't
matter.
"""
for row_label, v in attrs.iterrows():
for col_label, col in v.items():
i = self.index.get_indexer([row_label])[0]
j = self.columns.get_indexer([col_label])[0]
for pair in col.rstrip(";").split(";"):
coli = {k: i for i, k in enumerate(self.columns)}
rowi = {k: i for i, k in enumerate(self.index)}
for jj in range(len(attrs.columns)):
cn = attrs.columns[jj]
j = coli[cn]
for rn, c in attrs[[cn]].itertuples():
if not c:
continue
c = c.rstrip(";")
if not c:
continue
i = rowi[rn]
for pair in c.split(";"):
self.ctx[(i, j)].append(pair)

def _copy(self, deepcopy: bool = False) -> "Styler":
Expand Down
13 changes: 3 additions & 10 deletions pandas/tests/io/formats/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,10 @@ def f(x):

result = self.df.style.where(f, style1)._compute().ctx
expected = {
(r, c): [style1 if f(self.df.loc[row, col]) else ""]
(r, c): [style1]
for r, row in enumerate(self.df.index)
for c, col in enumerate(self.df.columns)
if f(self.df.loc[row, col])
}
assert result == expected

Expand Down Expand Up @@ -966,7 +967,6 @@ def test_bar_align_mid_nans(self):
"transparent 25.0%, #d65f5f 25.0%, "
"#d65f5f 50.0%, transparent 50.0%)",
],
(1, 0): [""],
(0, 1): [
"width: 10em",
" height: 80%",
Expand Down Expand Up @@ -994,7 +994,6 @@ def test_bar_align_zero_nans(self):
"transparent 50.0%, #d65f5f 50.0%, "
"#d65f5f 75.0%, transparent 75.0%)",
],
(1, 0): [""],
(0, 1): [
"width: 10em",
" height: 80%",
Expand Down Expand Up @@ -1091,7 +1090,7 @@ def test_format_with_bad_na_rep(self):
def test_highlight_null(self, null_color="red"):
df = pd.DataFrame({"A": [0, np.nan]})
result = df.style.highlight_null()._compute().ctx
expected = {(0, 0): [""], (1, 0): ["background-color: red"]}
expected = {(1, 0): ["background-color: red"]}
assert result == expected

def test_highlight_null_subset(self):
Expand All @@ -1104,9 +1103,7 @@ def test_highlight_null_subset(self):
.ctx
)
expected = {
(0, 0): [""],
(1, 0): ["background-color: red"],
(0, 1): [""],
(1, 1): ["background-color: green"],
}
assert result == expected
Expand Down Expand Up @@ -1219,17 +1216,13 @@ def test_highlight_max(self):
expected = {
(1, 0): ["background-color: yellow"],
(1, 1): ["background-color: yellow"],
(0, 1): [""],
(0, 0): [""],
}
assert result == expected

result = getattr(df.style, attr)(axis=1)._compute().ctx
expected = {
(0, 1): ["background-color: yellow"],
(1, 1): ["background-color: yellow"],
(0, 0): [""],
(1, 0): [""],
}
assert result == expected

Expand Down