Skip to content

BUG: overriden methods of subclasses of Styler are not called during rendering #52728 #52919

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 5 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ ExtensionArray

Styler
^^^^^^
-
- Bug in :meth:`Styler._copy` calling overridden methods in subclasses of :class:`Styler` (:issue:`52728`)
-

Other
Expand Down
21 changes: 13 additions & 8 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,9 @@ def to_latex(

.. figure:: ../../_static/style/latex_stocks.png
"""
obj = self._copy(deepcopy=True) # manipulate table_styles on obj, not self
obj = Styler._copy(
self, deepcopy=True
) # manipulate table_styles on obj, not self

table_selectors = (
[style["selector"] for style in self.table_styles]
Expand Down Expand Up @@ -1292,7 +1294,9 @@ def to_html(
--------
DataFrame.to_html: Write a DataFrame to a file, buffer or string in HTML format.
"""
obj = self._copy(deepcopy=True) # manipulate table_styles on obj, not self
obj = Styler._copy(
self, deepcopy=True
) # manipulate table_styles on obj, not self

if table_uuid:
obj.set_uuid(table_uuid)
Expand Down Expand Up @@ -1404,7 +1408,7 @@ def to_string(
str or None
If `buf` is None, returns the result as a string. Otherwise returns `None`.
"""
obj = self._copy(deepcopy=True)
obj = Styler._copy(self, deepcopy=True)

if sparse_index is None:
sparse_index = get_option("styler.sparse.index")
Expand Down Expand Up @@ -1554,7 +1558,8 @@ def _update_ctx_header(self, attrs: DataFrame, axis: AxisInt) -> None:
else:
self.ctx_columns[(j, i)].extend(css_list)

def _copy(self, deepcopy: bool = False) -> Styler:
@classmethod
def _copy(cls, self, deepcopy: bool = False) -> Styler:
"""
Copies a Styler, allowing for deepcopy or shallow copy

Expand All @@ -1578,8 +1583,8 @@ def _copy(self, deepcopy: bool = False) -> Styler:
- applied styles (_todo)

"""
# GH 40675
styler = Styler(
# GH 40675, 52728
styler = type(self)(
self.data, # populates attributes 'data', 'columns', 'index' as shallow
)
shallow = [ # simple string or boolean immutables
Expand Down Expand Up @@ -1624,10 +1629,10 @@ def _copy(self, deepcopy: bool = False) -> Styler:
return styler

def __copy__(self) -> Styler:
return self._copy(deepcopy=False)
return Styler._copy(self, deepcopy=False)

def __deepcopy__(self, memo) -> Styler:
return self._copy(deepcopy=True)
return Styler._copy(self, deepcopy=True)

def clear(self) -> None:
"""
Expand Down
16 changes: 15 additions & 1 deletion pandas/tests/io/formats/style/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def mi_styler(mi_df):
@pytest.fixture
def mi_styler_comp(mi_styler):
# comprehensively add features to mi_styler
mi_styler = mi_styler._copy(deepcopy=True)
mi_styler = Styler._copy(mi_styler, deepcopy=True)
mi_styler.css = {**mi_styler.css, "row": "ROW", "col": "COL"}
mi_styler.uuid_len = 5
mi_styler.uuid = "abcde"
Expand Down Expand Up @@ -313,6 +313,20 @@ def test_copy(comprehensive, render, deepcopy, mi_styler, mi_styler_comp):
assert id(getattr(s2, attr)) != id(getattr(styler, attr))


@pytest.mark.parametrize("deepcopy", [True, False])
def test_inherited_copy(mi_styler, deepcopy):
# Ensure that the inherited class is preserved when a Styler object is copied.
# GH 52728
class CustomStyler(Styler):
pass

custom_styler = CustomStyler(mi_styler.data)
custom_styler_copy = (
copy.deepcopy(custom_styler) if deepcopy else copy.copy(custom_styler)
)
assert isinstance(custom_styler_copy, CustomStyler)


def test_clear(mi_styler_comp):
# NOTE: if this test fails for new features then 'mi_styler_comp' should be updated
# to ensure proper testing of the 'copy', 'clear', 'export' methods with new feature
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/io/formats/style/test_to_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ def test_parse_latex_css_conversion_option():

def test_styler_object_after_render(styler):
# GH 42320
pre_render = styler._copy(deepcopy=True)
pre_render = Styler._copy(styler, deepcopy=True)
styler.to_latex(
column_format="rllr",
position="h",
Expand Down