diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index f33d56bbed6d6..2a588316cb7aa 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -65,6 +65,8 @@ Other enhancements - Support passing a :class:`Iterable[Hashable]` input to :meth:`DataFrame.drop_duplicates` (:issue:`59237`) - Support reading Stata 102-format (Stata 1) dta files (:issue:`58978`) - Support reading Stata 110-format (Stata 7) dta files (:issue:`47176`) +- The ``clines`` parameter to :meth:`Styler.to_latex` now accepts a tuple of option strings. The previous interface of passing a single string for multiple options is deprecated. +- :meth:`Styler.to_latex` accepts additional options to the ``clines`` parameter, allowing lines to be drawn between hidden index levels. .. --------------------------------------------------------------------------- .. _whatsnew_300.notable_bug_fixes: diff --git a/pandas/core/generic.py b/pandas/core/generic.py index de7fb3682fb4f..a7fbb56133ee5 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3582,7 +3582,7 @@ def _wrap(x, alt_format_): "label": label, "position": position, "column_format": column_format, - "clines": "skip-last;data" + "clines": ("skip-last", "rule-data") if (multirow and isinstance(self.index, MultiIndex)) else None, "bold_rows": bold_rows, diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index eb6773310da69..4b3720809f544 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -620,7 +620,7 @@ def to_latex( position: str | None = ..., position_float: str | None = ..., hrules: bool | None = ..., - clines: str | None = ..., + clines: str | tuple | None = ..., label: str | None = ..., caption: str | tuple | None = ..., sparse_index: bool | None = ..., @@ -642,7 +642,7 @@ def to_latex( position: str | None = ..., position_float: str | None = ..., hrules: bool | None = ..., - clines: str | None = ..., + clines: str | tuple | None = ..., label: str | None = ..., caption: str | tuple | None = ..., sparse_index: bool | None = ..., @@ -663,7 +663,7 @@ def to_latex( position: str | None = None, position_float: str | None = None, hrules: bool | None = None, - clines: str | None = None, + clines: str | tuple | None = None, label: str | None = None, caption: str | tuple | None = None, sparse_index: bool | None = None, @@ -712,22 +712,36 @@ def to_latex( Defaults to ``pandas.options.styler.latex.hrules``, which is `False`. .. versionchanged:: 1.4.0 - clines : str, optional + clines : str, tuple, optional Use to control adding \\cline commands for the index labels separation. - Possible values are: + If `None`, then no cline commands are added (default). + + If a tuple is passed, the following elements are recognised: + - `"rule-data"`: if present, clines are added for both the index and data. + Otherwise, they are only drawn for the index. + - `"skip-last"`: if present, the last index level is omitted when deciding + where to add clines. Otherwise, all index levels are used. + - `"include-hidden"`: if present, hidden index levels are included when + deciding where to add clines. Otherwise, only visible index levels are + used. + + If a str is passed, possible values are: - `None`: no cline commands are added (default). - - `"all;data"`: a cline is added for every index value extending the - width of the table, including data entries. + - `"all;data"`: a cline is added for every visible index value extending + the width of the table, including data entries. - `"all;index"`: as above with lines extending only the width of the index entries. - - `"skip-last;data"`: a cline is added for each index value except the - last level (which is never sparsified), extending the widtn of the + - `"skip-last;data"`: a cline is added for each visible index value except + the last level (which is never sparsified), extending the widtn of the table. - `"skip-last;index"`: as above with lines extending only the width of the index entries. - .. versionadded:: 1.4.0 + .. versionchanged:: 3.0.0 + .. deprecated:: 3.0.0 + Passing a str is deprecated and will be removed in a future version, + please use a tuple instead. label : str, optional The LaTeX label included as: \\label{