Skip to content

Commit aae0e16

Browse files
nicklausroachNicklaus RoachNicklaus Roach
authored and
Yi Wei
committed
BUG: overriden methods of subclasses of Styler are not called during rendering pandas-dev#52728 (pandas-dev#52919)
* use type() instead of __class__ * add test * remove unnecessary file * typo * revert change to class method --------- Co-authored-by: Nicklaus Roach <[email protected]> Co-authored-by: Nicklaus Roach <[email protected]>
1 parent cc5c2cc commit aae0e16

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

doc/source/whatsnew/v2.1.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ ExtensionArray
419419

420420
Styler
421421
^^^^^^
422-
-
422+
- Bug in :meth:`Styler._copy` calling overridden methods in subclasses of :class:`Styler` (:issue:`52728`)
423423
-
424424

425425
Other

pandas/io/formats/style.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1579,8 +1579,8 @@ def _copy(self, deepcopy: bool = False) -> Styler:
15791579
- applied styles (_todo)
15801580
15811581
"""
1582-
# GH 40675
1583-
styler = Styler(
1582+
# GH 40675, 52728
1583+
styler = type(self)(
15841584
self.data, # populates attributes 'data', 'columns', 'index' as shallow
15851585
)
15861586
shallow = [ # simple string or boolean immutables

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

+14
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,20 @@ def test_copy(comprehensive, render, deepcopy, mi_styler, mi_styler_comp):
313313
assert id(getattr(s2, attr)) != id(getattr(styler, attr))
314314

315315

316+
@pytest.mark.parametrize("deepcopy", [True, False])
317+
def test_inherited_copy(mi_styler, deepcopy):
318+
# Ensure that the inherited class is preserved when a Styler object is copied.
319+
# GH 52728
320+
class CustomStyler(Styler):
321+
pass
322+
323+
custom_styler = CustomStyler(mi_styler.data)
324+
custom_styler_copy = (
325+
copy.deepcopy(custom_styler) if deepcopy else copy.copy(custom_styler)
326+
)
327+
assert isinstance(custom_styler_copy, CustomStyler)
328+
329+
316330
def test_clear(mi_styler_comp):
317331
# NOTE: if this test fails for new features then 'mi_styler_comp' should be updated
318332
# to ensure proper testing of the 'copy', 'clear', 'export' methods with new feature

0 commit comments

Comments
 (0)