Skip to content

Commit 6a70067

Browse files
attack68Leonardofreua
authored andcommitted
ENH: sparse_columns and sparse_index added to Styler.to_html (pandas-dev#41946)
1 parent 994ff25 commit 6a70067

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

doc/source/whatsnew/v1.4.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Other enhancements
3535
- Additional options added to :meth:`.Styler.bar` to control alignment and display, with keyword only arguments (:issue:`26070`, :issue:`36419`)
3636
- :meth:`Styler.bar` now validates the input argument ``width`` and ``height`` (:issue:`42511`)
3737
- :meth:`Series.ewm`, :meth:`DataFrame.ewm`, now support a ``method`` argument with a ``'table'`` option that performs the windowing operation over an entire :class:`DataFrame`. See :ref:`Window Overview <window.overview>` for performance and functional benefits (:issue:`42273`)
38+
- Added ``sparse_index`` and ``sparse_columns`` keyword arguments to :meth:`.Styler.to_html` (:issue:`41946`)
3839
- Added keyword argument ``environment`` to :meth:`.Styler.to_latex` also allowing a specific "longtable" entry with a separate jinja2 template (:issue:`41866`)
3940
-
4041

pandas/io/formats/style.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -477,8 +477,8 @@ def to_latex(
477477
Defaults to ``pandas.options.styler.sparse.index`` value.
478478
sparse_columns : bool, optional
479479
Whether to sparsify the display of a hierarchical index. Setting to False
480-
will display each explicit level element in a hierarchical key for each row.
481-
Defaults to ``pandas.options.styler.sparse.columns`` value.
480+
will display each explicit level element in a hierarchical key for each
481+
column. Defaults to ``pandas.options.styler.sparse.columns`` value.
482482
multirow_align : {"c", "t", "b"}
483483
If sparsifying hierarchical MultiIndexes whether to align text centrally,
484484
at the top or bottom.
@@ -822,6 +822,8 @@ def to_html(
822822
*,
823823
table_uuid: str | None = None,
824824
table_attributes: str | None = None,
825+
sparse_index: bool | None = None,
826+
sparse_columns: bool | None = None,
825827
encoding: str | None = None,
826828
doctype_html: bool = False,
827829
exclude_styles: bool = False,
@@ -847,6 +849,18 @@ def to_html(
847849
``<table .. <table_attributes> >``
848850
849851
If not given defaults to Styler's preexisting value.
852+
sparse_index : bool, optional
853+
Whether to sparsify the display of a hierarchical index. Setting to False
854+
will display each explicit level element in a hierarchical key for each row.
855+
Defaults to ``pandas.options.styler.sparse.index`` value.
856+
857+
.. versionadded:: 1.4.0
858+
sparse_columns : bool, optional
859+
Whether to sparsify the display of a hierarchical index. Setting to False
860+
will display each explicit level element in a hierarchical key for each
861+
column. Defaults to ``pandas.options.styler.sparse.columns`` value.
862+
863+
.. versionadded:: 1.4.0
850864
encoding : str, optional
851865
Character encoding setting for file output, and HTML meta tags,
852866
defaults to "utf-8" if None.
@@ -873,8 +887,15 @@ def to_html(
873887
if table_attributes:
874888
self.set_table_attributes(table_attributes)
875889

890+
if sparse_index is None:
891+
sparse_index = get_option("styler.sparse.index")
892+
if sparse_columns is None:
893+
sparse_columns = get_option("styler.sparse.columns")
894+
876895
# Build HTML string..
877-
html = self.render(
896+
html = self._render_html(
897+
sparse_index=sparse_index,
898+
sparse_columns=sparse_columns,
878899
exclude_styles=exclude_styles,
879900
encoding=encoding if encoding else "utf-8",
880901
doctype_html=doctype_html,

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from pandas import (
77
DataFrame,
88
MultiIndex,
9+
option_context,
910
)
1011

1112
jinja2 = pytest.importorskip("jinja2")
@@ -429,3 +430,24 @@ def test_sticky_levels(styler_mi, index, columns):
429430
def test_sticky_raises(styler):
430431
with pytest.raises(ValueError, match="`axis` must be"):
431432
styler.set_sticky(axis="bad")
433+
434+
435+
@pytest.mark.parametrize(
436+
"sparse_index, sparse_columns",
437+
[(True, True), (True, False), (False, True), (False, False)],
438+
)
439+
def test_sparse_options(sparse_index, sparse_columns):
440+
cidx = MultiIndex.from_tuples([("Z", "a"), ("Z", "b"), ("Y", "c")])
441+
ridx = MultiIndex.from_tuples([("A", "a"), ("A", "b"), ("B", "c")])
442+
df = DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], index=ridx, columns=cidx)
443+
styler = df.style
444+
445+
default_html = styler.to_html() # defaults under pd.options to (True , True)
446+
447+
with option_context(
448+
"styler.sparse.index", sparse_index, "styler.sparse.columns", sparse_columns
449+
):
450+
html1 = styler.to_html()
451+
assert (html1 == default_html) is (sparse_index and sparse_columns)
452+
html2 = styler.to_html(sparse_index=sparse_index, sparse_columns=sparse_columns)
453+
assert html1 == html2

0 commit comments

Comments
 (0)