Skip to content

Commit 1a33966

Browse files
attack68CGe0516
authored andcommitted
ENH: sparse_columns and sparse_index added to Styler.to_html (pandas-dev#41946)
1 parent fc200e0 commit 1a33966

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

doc/source/whatsnew/v1.4.0.rst

+1
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

+24-3
Original file line numberDiff line numberDiff line change
@@ -476,8 +476,8 @@ def to_latex(
476476
Defaults to ``pandas.options.styler.sparse.index`` value.
477477
sparse_columns : bool, optional
478478
Whether to sparsify the display of a hierarchical index. Setting to False
479-
will display each explicit level element in a hierarchical key for each row.
480-
Defaults to ``pandas.options.styler.sparse.columns`` value.
479+
will display each explicit level element in a hierarchical key for each
480+
column. Defaults to ``pandas.options.styler.sparse.columns`` value.
481481
multirow_align : {"c", "t", "b"}
482482
If sparsifying hierarchical MultiIndexes whether to align text centrally,
483483
at the top or bottom.
@@ -815,6 +815,8 @@ def to_html(
815815
*,
816816
table_uuid: str | None = None,
817817
table_attributes: str | None = None,
818+
sparse_index: bool | None = None,
819+
sparse_columns: bool | None = None,
818820
encoding: str | None = None,
819821
doctype_html: bool = False,
820822
exclude_styles: bool = False,
@@ -840,6 +842,18 @@ def to_html(
840842
``<table .. <table_attributes> >``
841843
842844
If not given defaults to Styler's preexisting value.
845+
sparse_index : bool, optional
846+
Whether to sparsify the display of a hierarchical index. Setting to False
847+
will display each explicit level element in a hierarchical key for each row.
848+
Defaults to ``pandas.options.styler.sparse.index`` value.
849+
850+
.. versionadded:: 1.4.0
851+
sparse_columns : bool, optional
852+
Whether to sparsify the display of a hierarchical index. Setting to False
853+
will display each explicit level element in a hierarchical key for each
854+
column. Defaults to ``pandas.options.styler.sparse.columns`` value.
855+
856+
.. versionadded:: 1.4.0
843857
encoding : str, optional
844858
Character encoding setting for file output, and HTML meta tags,
845859
defaults to "utf-8" if None.
@@ -866,8 +880,15 @@ def to_html(
866880
if table_attributes:
867881
self.set_table_attributes(table_attributes)
868882

883+
if sparse_index is None:
884+
sparse_index = get_option("styler.sparse.index")
885+
if sparse_columns is None:
886+
sparse_columns = get_option("styler.sparse.columns")
887+
869888
# Build HTML string..
870-
html = self.render(
889+
html = self._render_html(
890+
sparse_index=sparse_index,
891+
sparse_columns=sparse_columns,
871892
exclude_styles=exclude_styles,
872893
encoding=encoding if encoding else "utf-8",
873894
doctype_html=doctype_html,

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

+22
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)