Skip to content

Commit 9289161

Browse files
attack68meeseeksmachine
authored andcommitted
Backport PR pandas-dev#42323: BUG: Styler.to_latex now doesn't manipulate the Styler object.
1 parent 061642a commit 9289161

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

doc/source/whatsnew/v1.3.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ which has been revised and improved (:issue:`39720`, :issue:`39317`, :issue:`404
136136
- Many features of the :class:`.Styler` class are now either partially or fully usable on a DataFrame with a non-unique indexes or columns (:issue:`41143`)
137137
- One has greater control of the display through separate sparsification of the index or columns using the :ref:`new styler options <options.available>`, which are also usable via :func:`option_context` (:issue:`41142`)
138138
- Added the option ``styler.render.max_elements`` to avoid browser overload when styling large DataFrames (:issue:`40712`)
139-
- Added the method :meth:`.Styler.to_latex` (:issue:`21673`), which also allows some limited CSS conversion (:issue:`40731`)
139+
- Added the method :meth:`.Styler.to_latex` (:issue:`21673`, :issue:`42320`), which also allows some limited CSS conversion (:issue:`40731`)
140140
- Added the method :meth:`.Styler.to_html` (:issue:`13379`)
141141
- Added the method :meth:`.Styler.set_sticky` to make index and column headers permanently visible in scrolling HTML frames (:issue:`29072`)
142142

pandas/io/formats/style.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,8 @@ def to_latex(
709709
0 & {\bfseries}{\Huge{1}} \\
710710
\end{tabular}
711711
"""
712+
obj = self._copy(deepcopy=True) # manipulate table_styles on obj, not self
713+
712714
table_selectors = (
713715
[style["selector"] for style in self.table_styles]
714716
if self.table_styles is not None
@@ -717,7 +719,7 @@ def to_latex(
717719

718720
if column_format is not None:
719721
# add more recent setting to table_styles
720-
self.set_table_styles(
722+
obj.set_table_styles(
721723
[{"selector": "column_format", "props": f":{column_format}"}],
722724
overwrite=False,
723725
)
@@ -735,13 +737,13 @@ def to_latex(
735737
column_format += (
736738
("r" if not siunitx else "S") if ci in numeric_cols else "l"
737739
)
738-
self.set_table_styles(
740+
obj.set_table_styles(
739741
[{"selector": "column_format", "props": f":{column_format}"}],
740742
overwrite=False,
741743
)
742744

743745
if position:
744-
self.set_table_styles(
746+
obj.set_table_styles(
745747
[{"selector": "position", "props": f":{position}"}],
746748
overwrite=False,
747749
)
@@ -753,13 +755,13 @@ def to_latex(
753755
f"'raggedright', 'raggedleft', 'centering', "
754756
f"got: '{position_float}'"
755757
)
756-
self.set_table_styles(
758+
obj.set_table_styles(
757759
[{"selector": "position_float", "props": f":{position_float}"}],
758760
overwrite=False,
759761
)
760762

761763
if hrules:
762-
self.set_table_styles(
764+
obj.set_table_styles(
763765
[
764766
{"selector": "toprule", "props": ":toprule"},
765767
{"selector": "midrule", "props": ":midrule"},
@@ -769,20 +771,20 @@ def to_latex(
769771
)
770772

771773
if label:
772-
self.set_table_styles(
774+
obj.set_table_styles(
773775
[{"selector": "label", "props": f":{{{label.replace(':', '§')}}}"}],
774776
overwrite=False,
775777
)
776778

777779
if caption:
778-
self.set_caption(caption)
780+
obj.set_caption(caption)
779781

780782
if sparse_index is None:
781783
sparse_index = get_option("styler.sparse.index")
782784
if sparse_columns is None:
783785
sparse_columns = get_option("styler.sparse.columns")
784786

785-
latex = self._render_latex(
787+
latex = obj._render_latex(
786788
sparse_index=sparse_index,
787789
sparse_columns=sparse_columns,
788790
multirow_align=multirow_align,

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

+16
Original file line numberDiff line numberDiff line change
@@ -489,3 +489,19 @@ def test_parse_latex_css_conversion_option():
489489
expected = [("command", "option--wrap")]
490490
result = _parse_latex_css_conversion(css)
491491
assert result == expected
492+
493+
494+
def test_styler_object_after_render(styler):
495+
# GH 42320
496+
pre_render = styler._copy(deepcopy=True)
497+
styler.to_latex(
498+
column_format="rllr",
499+
position="h",
500+
position_float="centering",
501+
hrules=True,
502+
label="my lab",
503+
caption="my cap",
504+
)
505+
506+
assert pre_render.table_styles == styler.table_styles
507+
assert pre_render.caption == styler.caption

0 commit comments

Comments
 (0)