Skip to content

Commit b968ef6

Browse files
committed
change single string parameter into tuple of strings
deprecate passing the single string parameter convert the single string parameter to the new tuple format in the interim
1 parent 9022f54 commit b968ef6

File tree

3 files changed

+179
-55
lines changed

3 files changed

+179
-55
lines changed

pandas/io/formats/style.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ def to_latex(
620620
position: str | None = ...,
621621
position_float: str | None = ...,
622622
hrules: bool | None = ...,
623-
clines: str | None = ...,
623+
clines: str | tuple | None = ...,
624624
label: str | None = ...,
625625
caption: str | tuple | None = ...,
626626
sparse_index: bool | None = ...,
@@ -642,7 +642,7 @@ def to_latex(
642642
position: str | None = ...,
643643
position_float: str | None = ...,
644644
hrules: bool | None = ...,
645-
clines: str | None = ...,
645+
clines: str | tuple | None = ...,
646646
label: str | None = ...,
647647
caption: str | tuple | None = ...,
648648
sparse_index: bool | None = ...,
@@ -663,7 +663,7 @@ def to_latex(
663663
position: str | None = None,
664664
position_float: str | None = None,
665665
hrules: bool | None = None,
666-
clines: str | None = None,
666+
clines: str | tuple | None = None,
667667
label: str | None = None,
668668
caption: str | tuple | None = None,
669669
sparse_index: bool | None = None,
@@ -712,32 +712,36 @@ def to_latex(
712712
Defaults to ``pandas.options.styler.latex.hrules``, which is `False`.
713713
714714
.. versionchanged:: 1.4.0
715-
clines : str, optional
715+
clines : str, tuple, optional
716716
Use to control adding \\cline commands for the index labels separation.
717-
Possible values are:
717+
If `None`, then no cline commands are added (default).
718+
719+
If a tuple is passed, the following elements are recognised:
720+
- `"rule-data"`: if present, clines are added for both the index and data.
721+
Otherwise, they are only drawn for the index.
722+
- `"skip-last"`: if present, the last index level is omitted when deciding
723+
where to add clines. Otherwise, all index levels are used.
724+
- `"include-hidden"`: if present, hidden index levels are included when
725+
deciding where to add clines. Otherwise, only visible index levels are
726+
used.
727+
728+
If a str is passed, possible values are:
718729
719730
- `None`: no cline commands are added (default).
720731
- `"all;data"`: a cline is added for every visible index value extending
721732
the width of the table, including data entries.
722733
- `"all;index"`: as above with lines extending only the width of the
723734
index entries.
724-
- `"all-invisible;data"`: a cline is added for every index value,
725-
including hidden indexes, extending the full width of the table,
726-
including data entries.
727-
- `"all-invisible;index"`: as above with lines extending only the width
728-
of the index entries.
729735
- `"skip-last;data"`: a cline is added for each visible index value except
730736
the last level (which is never sparsified), extending the widtn of the
731737
table.
732738
- `"skip-last;index"`: as above with lines extending only the width of the
733739
index entries.
734-
- `"skip-last-invisible;data"`: a cline is added for each index value,
735-
including hidden index levels, but excluding the last (which is never
736-
sparsified), extending the width of the table.
737-
- `"skip-last-invisible;index"`: as above with lines extending only the
738-
width of the index entries.
739740
740741
.. versionchanged:: 3.0.0
742+
.. deprecated:: 3.0.0
743+
Passing a str is deprecated and will be removed in a future version,
744+
please use a tuple instead.
741745
label : str, optional
742746
The LaTeX label included as: \\label{<label>}.
743747
This is used with \\ref{<label>} in the main .tex file.
@@ -1090,7 +1094,7 @@ def to_latex(
10901094
10911095
>>> styler.to_latex(
10921096
... caption="Selected stock correlation and simple statistics.",
1093-
... clines="skip-last;data",
1097+
... clines=("skip-last", "across-data"),
10941098
... convert_css=True,
10951099
... position_float="centering",
10961100
... multicol_align="|c|",

pandas/io/formats/style_render.py

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616
Union,
1717
)
1818
from uuid import uuid4
19+
import warnings
1920

2021
import numpy as np
2122

2223
from pandas._config import get_option
2324

2425
from pandas._libs import lib
2526
from pandas.compat._optional import import_optional_dependency
27+
from pandas.util._exceptions import find_stack_level
2628

2729
from pandas.core.dtypes.common import (
2830
is_complex,
@@ -218,7 +220,11 @@ def _render_html(
218220
)
219221

220222
def _render_latex(
221-
self, sparse_index: bool, sparse_columns: bool, clines: str | None, **kwargs
223+
self,
224+
sparse_index: bool,
225+
sparse_columns: bool,
226+
clines: str | tuple | None,
227+
**kwargs,
222228
) -> str:
223229
"""
224230
Render a Styler in latex format
@@ -857,7 +863,33 @@ def _generate_body_row(
857863

858864
return index_headers + data
859865

860-
def _translate_latex(self, d: dict, clines: str | None) -> None:
866+
def _convert_clines_to_tuple(self, clines: str | tuple | None) -> tuple | None:
867+
if not isinstance(clines, str):
868+
return clines
869+
870+
msg = (
871+
"Passing a string argument to the clines parameter is deprecated and will "
872+
"be removed in a future version, please use a tuple instead, "
873+
"e.g. ('rule-data', 'skip-last') instead of 'skip-last;data'."
874+
)
875+
warnings.warn(msg, FutureWarning, stacklevel=find_stack_level())
876+
877+
if clines not in ["all;data", "all;index", "skip-last;data", "skip-last;index"]:
878+
raise ValueError(
879+
f"`clines` value of {clines} is invalid. Should either be None, "
880+
"a tuple, or one of 'all;data', 'all;index', 'skip-last;data', "
881+
"'skip-last;index'."
882+
)
883+
884+
result = ()
885+
if "data" in clines:
886+
result += ("rule-data",)
887+
if "skip-last" in clines:
888+
result += ("skip-last",)
889+
890+
return result
891+
892+
def _translate_latex(self, d: dict, clines: str | tuple | None) -> None:
861893
r"""
862894
Post-process the default render dict for the LaTeX template format.
863895
@@ -867,6 +899,7 @@ def _translate_latex(self, d: dict, clines: str | None) -> None:
867899
- Remove hidden indexes or reinsert missing th elements if part of multiindex
868900
or multirow sparsification (so that \multirow and \multicol work correctly).
869901
"""
902+
clines = self._convert_clines_to_tuple(clines)
870903
index_levels = self.index.nlevels
871904
# GH 52218
872905
visible_index_level_n = max(1, index_levels - sum(self.hide_index_))
@@ -929,25 +962,15 @@ def concatenated_visible_rows(obj):
929962

930963
# clines are determined from info on index_lengths and hidden_rows and input
931964
# to a dict defining which row clines should be added in the template.
932-
if clines not in [
933-
None,
934-
"all;data",
935-
"all;index",
936-
"all-invisible;data",
937-
"all-invisible;index",
938-
"skip-last;data",
939-
"skip-last;index",
940-
"skip-last-invisible;data",
941-
"skip-last-invisible;index",
942-
]:
943-
raise ValueError(
944-
f"`clines` value of {clines} is invalid. Should either be None or one "
945-
f"of 'all;data', 'all;index', 'all-invisible;data', "
946-
f"'all-invisible;index', 'skip-last;data', 'skip-last;index', "
947-
f"'skip-last-invisible;data', 'skip-last-invisible;index'."
948-
)
949965
if clines is not None:
950-
data_len = len(row_body_cells) if "data" in clines and d["body"] else 0
966+
valid_clines_options = ["skip-last", "rule-data", "include-hidden"]
967+
for option in clines:
968+
if option not in valid_clines_options:
969+
raise ValueError(
970+
f"`clines` option of {option} is invalid. "
971+
f"Choose one of {valid_clines_options}"
972+
)
973+
data_len = len(row_body_cells) if "rule-data" in clines and d["body"] else 0
951974

952975
d["clines"] = defaultdict(list)
953976
visible_row_indexes: list[int] = [
@@ -959,7 +982,7 @@ def concatenated_visible_rows(obj):
959982
for rn, r in enumerate(visible_row_indexes):
960983
lvln = 0
961984
for lvl in range(index_levels):
962-
if self.hide_index_[lvl] and "invisible" not in clines:
985+
if self.hide_index_[lvl] and "include-hidden" not in clines:
963986
continue
964987
if lvl == index_levels - 1 and "skip-last" in clines:
965988
continue

0 commit comments

Comments
 (0)