Skip to content

Commit e64b7ee

Browse files
authored
CLN: simplify Styler clear mechanics and tests (#42271)
1 parent ce83026 commit e64b7ee

File tree

2 files changed

+40
-36
lines changed

2 files changed

+40
-36
lines changed

pandas/io/formats/style.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -1031,15 +1031,14 @@ def clear(self) -> None:
10311031
10321032
Returns None.
10331033
"""
1034-
self.ctx.clear()
1035-
self.tooltips = None
1036-
self.cell_context.clear()
1037-
self._todo.clear()
1038-
1039-
self.hide_index_ = False
1040-
self.hidden_columns = []
1041-
# self.format and self.table_styles may be dependent on user
1042-
# input in self.__init__()
1034+
# create default GH 40675
1035+
clean_copy = Styler(self.data, uuid=self.uuid)
1036+
clean_attrs = [a for a in clean_copy.__dict__ if not callable(a)]
1037+
self_attrs = [a for a in self.__dict__ if not callable(a)] # maybe more attrs
1038+
for attr in clean_attrs:
1039+
setattr(self, attr, getattr(clean_copy, attr))
1040+
for attr in set(self_attrs).difference(clean_attrs):
1041+
delattr(self, attr)
10431042

10441043
def _apply(
10451044
self,

pandas/tests/io/formats/style/test_style.py

+32-27
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,38 @@ def test_copy(comprehensive, render, deepcopy, mi_styler, mi_styler_comp):
228228
assert id(getattr(s2, attr)) != id(getattr(styler, attr))
229229

230230

231+
def test_clear(mi_styler_comp):
232+
# NOTE: if this test fails for new features then 'mi_styler_comp' should be updated
233+
# to ensure proper testing of the 'copy', 'clear', 'export' methods with new feature
234+
# GH 40675
235+
styler = mi_styler_comp
236+
styler.to_html() # new attrs maybe created on render
237+
238+
clean_copy = Styler(styler.data, uuid=styler.uuid)
239+
240+
excl = [
241+
"data",
242+
"index",
243+
"columns",
244+
"uuid",
245+
"uuid_len",
246+
"cell_ids",
247+
"cellstyle_map", # execution time only
248+
"precision", # deprecated
249+
"na_rep", # deprecated
250+
]
251+
# tests vars are not same vals on obj and clean copy before clear (except for excl)
252+
for attr in [a for a in styler.__dict__ if not (callable(a) or a in excl)]:
253+
res = getattr(styler, attr) == getattr(clean_copy, attr)
254+
assert not (all(res) if (hasattr(res, "__iter__") and len(res) > 0) else res)
255+
256+
# test vars have same vales on obj and clean copy after clearing
257+
styler.clear()
258+
for attr in [a for a in styler.__dict__ if not (callable(a))]:
259+
res = getattr(styler, attr) == getattr(clean_copy, attr)
260+
assert all(res) if hasattr(res, "__iter__") else res
261+
262+
231263
class TestStyler:
232264
def setup_method(self, method):
233265
np.random.seed(24)
@@ -283,33 +315,6 @@ def test_update_ctx_flatten_multi_and_trailing_semi(self):
283315
}
284316
assert self.styler.ctx == expected
285317

286-
def test_clear(self):
287-
# updated in GH 39396
288-
tt = DataFrame({"A": [None, "tt"]})
289-
css = DataFrame({"A": [None, "cls-a"]})
290-
s = self.df.style.highlight_max().set_tooltips(tt).set_td_classes(css)
291-
s = s.hide_index().hide_columns("A")
292-
# _todo, tooltips and cell_context items added to..
293-
assert len(s._todo) > 0
294-
assert s.tooltips
295-
assert len(s.cell_context) > 0
296-
assert s.hide_index_ is True
297-
assert len(s.hidden_columns) > 0
298-
299-
s = s._compute()
300-
# ctx item affected when a render takes place. _todo is maintained
301-
assert len(s.ctx) > 0
302-
assert len(s._todo) > 0
303-
304-
s.clear()
305-
# ctx, _todo, tooltips and cell_context items all revert to null state.
306-
assert len(s.ctx) == 0
307-
assert len(s._todo) == 0
308-
assert not s.tooltips
309-
assert len(s.cell_context) == 0
310-
assert s.hide_index_ is False
311-
assert len(s.hidden_columns) == 0
312-
313318
def test_render(self):
314319
df = DataFrame({"A": [0, 1]})
315320
style = lambda x: pd.Series(["color: red", "color: blue"], name=x.name)

0 commit comments

Comments
 (0)