Skip to content

Commit 697ac5b

Browse files
committed
to speed up rendering of styler
see #19917
1 parent b687cd4 commit 697ac5b

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

pandas/io/formats/style.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -556,11 +556,15 @@ def _update_ctx(self, attrs):
556556
Whitespace shouldn't matter and the final trailing ';' shouldn't
557557
matter.
558558
"""
559-
for row_label, v in attrs.iterrows():
560-
for col_label, col in v.items():
561-
i = self.index.get_indexer([row_label])[0]
562-
j = self.columns.get_indexer([col_label])[0]
563-
for pair in col.rstrip(";").split(";"):
559+
rows = [(row_label, v) for row_label, v in attrs.iterrows()]
560+
row_idx = self.index.get_indexer([x[0] for x in rows])
561+
for ii, row in enumerate(rows):
562+
i = row_idx[ii]
563+
cols = [(col_label, col) for col_label, col in row[1].items() if col]
564+
col_idx = self.columns.get_indexer([x[0] for x in cols])
565+
for jj, itm in enumerate(cols):
566+
j = col_idx[jj]
567+
for pair in itm[1].rstrip(";").split(";"):
564568
self.ctx[(i, j)].append(pair)
565569

566570
def _copy(self, deepcopy=False):

pandas/tests/io/formats/test_style.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -404,9 +404,10 @@ def f(x):
404404

405405
result = self.df.style.where(f, style1)._compute().ctx
406406
expected = {
407-
(r, c): [style1 if f(self.df.loc[row, col]) else ""]
407+
(r, c): [style1]
408408
for r, row in enumerate(self.df.index)
409409
for c, col in enumerate(self.df.columns)
410+
if f(self.df.loc[row, col])
410411
}
411412
assert result == expected
412413

@@ -954,7 +955,6 @@ def test_bar_align_mid_nans(self):
954955
"transparent 25.0%, #d65f5f 25.0%, "
955956
"#d65f5f 50.0%, transparent 50.0%)",
956957
],
957-
(1, 0): [""],
958958
(0, 1): [
959959
"width: 10em",
960960
" height: 80%",
@@ -982,7 +982,6 @@ def test_bar_align_zero_nans(self):
982982
"transparent 50.0%, #d65f5f 50.0%, "
983983
"#d65f5f 75.0%, transparent 75.0%)",
984984
],
985-
(1, 0): [""],
986985
(0, 1): [
987986
"width: 10em",
988987
" height: 80%",
@@ -1077,7 +1076,7 @@ def test_format_with_bad_na_rep(self):
10771076
def test_highlight_null(self, null_color="red"):
10781077
df = pd.DataFrame({"A": [0, np.nan]})
10791078
result = df.style.highlight_null()._compute().ctx
1080-
expected = {(0, 0): [""], (1, 0): ["background-color: red"]}
1079+
expected = {(1, 0): ["background-color: red"]}
10811080
assert result == expected
10821081

10831082
def test_nonunique_raises(self):
@@ -1187,17 +1186,13 @@ def test_highlight_max(self):
11871186
expected = {
11881187
(1, 0): ["background-color: yellow"],
11891188
(1, 1): ["background-color: yellow"],
1190-
(0, 1): [""],
1191-
(0, 0): [""],
11921189
}
11931190
assert result == expected
11941191

11951192
result = getattr(df.style, attr)(axis=1)._compute().ctx
11961193
expected = {
11971194
(0, 1): ["background-color: yellow"],
11981195
(1, 1): ["background-color: yellow"],
1199-
(0, 0): [""],
1200-
(1, 0): [""],
12011196
}
12021197
assert result == expected
12031198

0 commit comments

Comments
 (0)