Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
58519d0
TST: Fix doctests for pandas.io.formats.style
Leonardofreua Jul 28, 2021
bb7c3b2
TST: Add link to redirect to Table Visualization user guide
Leonardofreua Jul 28, 2021
fa5a615
TST: Add image to pipe function result
Leonardofreua Jul 28, 2021
378c1f7
TST: Remove unnecessary outputs
Leonardofreua Jul 30, 2021
9dc52e4
TST: Add the output to the Styler.format doctest in to_latex()
Leonardofreua Jul 30, 2021
63535ce
REG: DataFrame.agg where func returns lists and axis=1 (#42762)
rhshadrach Jul 28, 2021
9d72fe0
Fix typing issues for CI (#42770)
Dr-Irv Jul 28, 2021
5ba05f5
BUG: groupby.shift returns different columns when fill_value is speci…
smithto1 Jul 28, 2021
994ff25
PERF: extract_array earlier in DataFrame construction (#42774)
jbrockmendel Jul 28, 2021
6a70067
ENH: `sparse_columns` and `sparse_index` added to `Styler.to_html` (…
attack68 Jul 28, 2021
26e2cd9
TYP: Fix typing for searchsorted (#42788)
rhshadrach Jul 29, 2021
05e0c24
DOC GH42756 Update documentation for pandas.DataFrame.drop to clarify…
Jul 29, 2021
a5d951e
CI: Fix doctests (#42790)
rhshadrach Jul 29, 2021
ebaa9c1
REGR: nanosecond timestamp comparisons to OOB datetimes (#42796)
mzeitlin11 Jul 29, 2021
84f3302
COMPAT: MPL 3.4.0 (#42803)
lithomas1 Jul 29, 2021
76162a4
Delete duplicates and unused code from reshape tests (#42802)
phofl Jul 30, 2021
2c21525
REGR: ValueError raised when both prefix and names are set to None (#…
lithomas1 Jul 30, 2021
13100d8
TST: Add style.py to the doctest check
Leonardofreua Jul 30, 2021
adc540f
TST: fixed eng_formatter doctest for #42671 (#42705)
KrishnaSai2020 Jul 30, 2021
9a35c94
Merge branch 'master' into fix-style-doctests
Leonardofreua Jul 30, 2021
4654dfc
TST: Revert x and y position in some doctests
Leonardofreua Jul 30, 2021
4fe1bea
Merge branch 'fix-style-doctests' of https://github.com/Leonardofreua…
Leonardofreua Jul 30, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
ENH: sparse_columns and sparse_index added to Styler.to_html (#…
  • Loading branch information
attack68 authored and Leonardofreua committed Jul 30, 2021
commit 6a70067c24b6b5be604a5214b0641dcff058c838
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Other enhancements
- Additional options added to :meth:`.Styler.bar` to control alignment and display, with keyword only arguments (:issue:`26070`, :issue:`36419`)
- :meth:`Styler.bar` now validates the input argument ``width`` and ``height`` (:issue:`42511`)
- :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`)
- Added ``sparse_index`` and ``sparse_columns`` keyword arguments to :meth:`.Styler.to_html` (:issue:`41946`)
- Added keyword argument ``environment`` to :meth:`.Styler.to_latex` also allowing a specific "longtable" entry with a separate jinja2 template (:issue:`41866`)
-

Expand Down
27 changes: 24 additions & 3 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,8 @@ def to_latex(
Defaults to ``pandas.options.styler.sparse.index`` value.
sparse_columns : bool, optional
Whether to sparsify the display of a hierarchical index. Setting to False
will display each explicit level element in a hierarchical key for each row.
Defaults to ``pandas.options.styler.sparse.columns`` value.
will display each explicit level element in a hierarchical key for each
column. Defaults to ``pandas.options.styler.sparse.columns`` value.
multirow_align : {"c", "t", "b"}
If sparsifying hierarchical MultiIndexes whether to align text centrally,
at the top or bottom.
Expand Down Expand Up @@ -822,6 +822,8 @@ def to_html(
*,
table_uuid: str | None = None,
table_attributes: str | None = None,
sparse_index: bool | None = None,
sparse_columns: bool | None = None,
encoding: str | None = None,
doctype_html: bool = False,
exclude_styles: bool = False,
Expand All @@ -847,6 +849,18 @@ def to_html(
``<table .. <table_attributes> >``

If not given defaults to Styler's preexisting value.
sparse_index : bool, optional
Whether to sparsify the display of a hierarchical index. Setting to False
will display each explicit level element in a hierarchical key for each row.
Defaults to ``pandas.options.styler.sparse.index`` value.

.. versionadded:: 1.4.0
sparse_columns : bool, optional
Whether to sparsify the display of a hierarchical index. Setting to False
will display each explicit level element in a hierarchical key for each
column. Defaults to ``pandas.options.styler.sparse.columns`` value.

.. versionadded:: 1.4.0
encoding : str, optional
Character encoding setting for file output, and HTML meta tags,
defaults to "utf-8" if None.
Expand All @@ -873,8 +887,15 @@ def to_html(
if table_attributes:
self.set_table_attributes(table_attributes)

if sparse_index is None:
sparse_index = get_option("styler.sparse.index")
if sparse_columns is None:
sparse_columns = get_option("styler.sparse.columns")

# Build HTML string..
html = self.render(
html = self._render_html(
sparse_index=sparse_index,
sparse_columns=sparse_columns,
exclude_styles=exclude_styles,
encoding=encoding if encoding else "utf-8",
doctype_html=doctype_html,
Expand Down
22 changes: 22 additions & 0 deletions pandas/tests/io/formats/style/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pandas import (
DataFrame,
MultiIndex,
option_context,
)

jinja2 = pytest.importorskip("jinja2")
Expand Down Expand Up @@ -429,3 +430,24 @@ def test_sticky_levels(styler_mi, index, columns):
def test_sticky_raises(styler):
with pytest.raises(ValueError, match="`axis` must be"):
styler.set_sticky(axis="bad")


@pytest.mark.parametrize(
"sparse_index, sparse_columns",
[(True, True), (True, False), (False, True), (False, False)],
)
def test_sparse_options(sparse_index, sparse_columns):
cidx = MultiIndex.from_tuples([("Z", "a"), ("Z", "b"), ("Y", "c")])
ridx = MultiIndex.from_tuples([("A", "a"), ("A", "b"), ("B", "c")])
df = DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], index=ridx, columns=cidx)
styler = df.style

default_html = styler.to_html() # defaults under pd.options to (True , True)

with option_context(
"styler.sparse.index", sparse_index, "styler.sparse.columns", sparse_columns
):
html1 = styler.to_html()
assert (html1 == default_html) is (sparse_index and sparse_columns)
html2 = styler.to_html(sparse_index=sparse_index, sparse_columns=sparse_columns)
assert html1 == html2