diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index e245bf797d932..dfed7fcf82039 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -610,7 +610,7 @@ Other - Bug in :func:`pandas.api.types.infer_dtype` not recognizing Series, Index or array with a period dtype (:issue:`23553`) - Bug in :func:`pandas.api.types.infer_dtype` raising an error for general :class:`.ExtensionArray` objects. It will now return ``"unknown-array"`` instead of raising (:issue:`37367`) - Bug in constructing a :class:`Series` from a list and a :class:`PandasDtype` (:issue:`39357`) -- Bug in :class:`Styler` which caused CSS to duplicate on multiple renders. (:issue:`39395`) +- Bug in :class:`Styler` which caused CSS to duplicate on multiple renders. (:issue:`39395`, :issue:`40334`) - ``inspect.getmembers(Series)`` no longer raises an ``AbstractMethodError`` (:issue:`38782`) - Bug in :meth:`Series.where` with numeric dtype and ``other = None`` not casting to ``nan`` (:issue:`39761`) - :meth:`Index.where` behavior now mirrors :meth:`Index.putmask` behavior, i.e. ``index.where(mask, other)`` matches ``index.putmask(~mask, other)`` (:issue:`39412`) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index af8c4821df755..ec09a4cc4cd89 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -855,10 +855,10 @@ def _compute(self): (application method, *args, **kwargs) """ + self.ctx.clear() r = self for func, args, kwargs in self._todo: r = func(self)(*args, **kwargs) - self._todo = [] return r def _apply( diff --git a/pandas/tests/io/formats/style/test_style.py b/pandas/tests/io/formats/style/test_style.py index b938495ca9e31..ca453b55eae2e 100644 --- a/pandas/tests/io/formats/style/test_style.py +++ b/pandas/tests/io/formats/style/test_style.py @@ -179,11 +179,10 @@ def test_clear(self): assert len(s.cell_context) > 0 s = s._compute() - # ctx and _todo items affected when a render takes place + # ctx item affected when a render takes place. _todo is maintained assert len(s.ctx) > 0 - assert len(s._todo) == 0 # _todo is emptied after compute. + assert len(s._todo) > 0 - s._todo = [1] s.clear() # ctx, _todo, tooltips and cell_context items all revert to null state. assert len(s.ctx) == 0 @@ -767,7 +766,7 @@ def test_export(self): f = lambda x: "color: red" if x > 0 else "color: blue" g = lambda x, z: f"color: {z}" if x > 0 else f"color: {z}" style1 = self.styler - style1.applymap(f).applymap(g, z="b").highlight_max() + style1.applymap(f).applymap(g, z="b").highlight_max()._compute() # = render result = style1.export() style2 = self.df.style style2.use(result)