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 9 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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,4 @@ doc/build/html/index.html
doc/tmp.sv
env/
doc/source/savefig/
virtualenvs/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert this addition to the gitignore. it is effective only for your environment.

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 @@ -145,6 +145,7 @@ Other enhancements
- :class:`RangeIndex` can now be constructed by passing a ``range`` object directly e.g. ``pd.RangeIndex(range(3))`` (:issue:`12067`)
- :meth:`round` being enabled for the nullable integer and floating dtypes (:issue:`38844`)
- :meth:`pandas.read_csv` and :meth:`pandas.read_json` expose the argument ``encoding_errors`` to control how encoding errors are handled (:issue:`39450`)
- :meth:`.Styler.clear` now clears :attr:`Styler.hidden_index`, :attr:`Styler.hidden_columns` and :attr:`Styler.tooltips` as well (:issue:`40484`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove tooltips since it already does this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this user visible at all?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a user visible change only if:

  • the user has a styler which has a hidden index, or hidden columns applied, or both,
  • then uses the clear method,
  • then re-renders the Styler

prior to this PR the hidden items would still be hidden, after this PR the styler resets so nothing is hidden.


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

Expand Down
8 changes: 7 additions & 1 deletion pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,13 @@ 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.tooltips = None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.tooltips = None is already set above and tested for

# 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