Skip to content

BUG: Update Styler.clear method to clear all #40664

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 12 commits into from
Apr 2, 2021
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 doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ Other enhancements
- :meth:`.Styler.apply` now more consistently accepts ndarray function returns, i.e. in all cases for ``axis`` is ``0, 1 or None`` (:issue:`39359`)
- :meth:`.Styler.apply` and :meth:`.Styler.applymap` now raise errors if wrong format CSS is passed on render (:issue:`39660`)
- :meth:`.Styler.format` adds keyword argument ``escape`` for optional HTML escaping (:issue:`40437`)
- :meth:`.Styler.clear` now clears :attr:`Styler.hidden_index` and :attr:`Styler.hidden_columns` as well (:issue:`40484`)
- Builtin highlighting methods in :class:`Styler` have a more consistent signature and css customisability (:issue:`40242`)
- :meth:`Series.loc.__getitem__` and :meth:`Series.loc.__setitem__` with :class:`MultiIndex` now raising helpful error message when indexer has too many dimensions (:issue:`35349`)
- :meth:`pandas.read_stata` and :class:`StataReader` support reading data from compressed files.
Expand Down
7 changes: 6 additions & 1 deletion pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,12 @@ def clear(self) -> None:
self.ctx.clear()
self.tooltips = None
self.cell_context.clear()
self._todo = []
self._todo.clear()

self.hidden_index = False
self.hidden_columns = []
# self.format and self.table_styles may be dependent on user
# input in self.__init__()

def _compute(self):
"""
Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/io/formats/style/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,13 @@ def test_clear(self):
tt = DataFrame({"A": [None, "tt"]})
css = DataFrame({"A": [None, "cls-a"]})
s = self.df.style.highlight_max().set_tooltips(tt).set_td_classes(css)
s = s.hide_index().hide_columns("A")
# _todo, tooltips and cell_context items added to..
assert len(s._todo) > 0
assert s.tooltips
assert len(s.cell_context) > 0
assert s.hidden_index is True
assert len(s.hidden_columns) > 0

s = s._compute()
# ctx item affected when a render takes place. _todo is maintained
Expand All @@ -190,6 +193,8 @@ def test_clear(self):
assert len(s._todo) == 0
assert not s.tooltips
assert len(s.cell_context) == 0
assert s.hidden_index is False
assert len(s.hidden_columns) == 0

def test_render(self):
df = DataFrame({"A": [0, 1]})
Expand Down