Skip to content

Commit b7edf80

Browse files
committed
add base changes
1 parent e7efcca commit b7edf80

File tree

4 files changed

+103
-2
lines changed

4 files changed

+103
-2
lines changed

doc/source/reference/style.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Style application
3939
Styler.apply_index
4040
Styler.applymap_index
4141
Styler.format
42+
Styler.format_index
4243
Styler.hide_index
4344
Styler.hide_columns
4445
Styler.set_td_classes

pandas/io/formats/style.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,6 +1184,8 @@ def _copy(self, deepcopy: bool = False) -> Styler:
11841184
]
11851185
deep = [ # nested lists or dicts
11861186
"_display_funcs",
1187+
"_display_funcs_index",
1188+
"_display_funcs_columns",
11871189
"hidden_rows",
11881190
"hidden_columns",
11891191
"ctx",

pandas/io/formats/style_render.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ def __init__(
117117
self._display_funcs: DefaultDict[ # maps (row, col) -> format func
118118
tuple[int, int], Callable[[Any], str]
119119
] = defaultdict(lambda: partial(_default_formatter, precision=precision))
120+
self._display_funcs_index: DefaultDict[ # maps (row, level) -> format func
121+
tuple[int, int], Callable[[Any], str]
122+
] = defaultdict(lambda: partial(_default_formatter, precision=precision))
123+
self._display_funcs_columns: DefaultDict[ # maps (level, col) -> format func
124+
tuple[int, int], Callable[[Any], str]
125+
] = defaultdict(lambda: partial(_default_formatter, precision=precision))
120126

121127
def _render_html(
122128
self,
@@ -377,6 +383,7 @@ def _translate_header(
377383
f"{col_heading_class} level{r} col{c}",
378384
value,
379385
_is_visible(c, r, col_lengths),
386+
display_value=self._display_funcs_columns[(r, c)](value),
380387
attributes=(
381388
f'colspan="{col_lengths.get((r, c), 0)}"'
382389
if col_lengths.get((r, c), 0) > 1
@@ -535,6 +542,7 @@ def _translate_body(
535542
f"{row_heading_class} level{c} row{r}",
536543
value,
537544
_is_visible(r, c, idx_lengths) and not self.hide_index_[c],
545+
display_value=self._display_funcs_index[(r, c)](value),
538546
attributes=(
539547
f'rowspan="{idx_lengths.get((c, r), 0)}"'
540548
if idx_lengths.get((c, r), 0) > 1
@@ -834,6 +842,96 @@ def format(
834842

835843
return self
836844

845+
def format_index(
846+
self,
847+
formatter: ExtFormatter | None = None,
848+
axis: int | str = 0,
849+
level: Level | list[Level] | None = None,
850+
na_rep: str | None = None,
851+
precision: int | None = None,
852+
decimal: str = ".",
853+
thousands: str | None = None,
854+
escape: str | None = None,
855+
) -> StylerRenderer:
856+
r"""
857+
Format the text display value of index labels or column headers.
858+
859+
.. versionadded:: 1.4.0
860+
861+
Parameters
862+
----------
863+
formatter : str, callable, dict or None
864+
Object to define how values are displayed. See notes.
865+
axis : {0, "index", 1, "columns"}
866+
Whether to apply the formatter to the index or column headers.
867+
level : int, str, list
868+
The level(s) over which to apply the generic formatter.
869+
na_rep : str, optional
870+
Representation for missing values.
871+
If ``na_rep`` is None, no special formatting is applied.
872+
precision : int, optional
873+
Floating point precision to use for display purposes, if not determined by
874+
the specified ``formatter``.
875+
decimal : str, default "."
876+
Character used as decimal separator for floats, complex and integers
877+
thousands : str, optional, default None
878+
Character used as thousands separator for floats, complex and integers
879+
escape : str, optional
880+
Use 'html' to replace the characters ``&``, ``<``, ``>``, ``'``, and ``"``
881+
in cell display string with HTML-safe sequences.
882+
Use 'latex' to replace the characters ``&``, ``%``, ``$``, ``#``, ``_``,
883+
``{``, ``}``, ``~``, ``^``, and ``\`` in the cell display string with
884+
LaTeX-safe sequences.
885+
Escaping is done before ``formatter``.
886+
887+
Returns
888+
-------
889+
self : Styler
890+
"""
891+
axis = self.data._get_axis_number(axis)
892+
if axis == 0:
893+
display_funcs_, obj = self._display_funcs_index, self.index
894+
else:
895+
display_funcs_, obj = self._display_funcs_columns, self.columns
896+
levels_ = refactor_levels(level, obj)
897+
898+
if all(
899+
(
900+
formatter is None,
901+
level is None,
902+
precision is None,
903+
decimal == ".",
904+
thousands is None,
905+
na_rep is None,
906+
escape is None,
907+
)
908+
):
909+
display_funcs_.clear()
910+
return self # clear the formatter / revert to default and avoid looping
911+
912+
if not isinstance(formatter, dict):
913+
formatter = {level: formatter for level in levels_}
914+
else:
915+
formatter = {
916+
obj._get_level_number(level): formatter_
917+
for level, formatter_ in formatter.items()
918+
}
919+
920+
for lvl in levels_:
921+
format_func = _maybe_wrap_formatter(
922+
formatter.get(lvl),
923+
na_rep=na_rep,
924+
precision=precision,
925+
decimal=decimal,
926+
thousands=thousands,
927+
escape=escape,
928+
)
929+
930+
for idx in [(i, lvl) if axis == 0 else (lvl, i) for i in range(len(obj))]:
931+
display_funcs_[idx] = format_func
932+
933+
return self
934+
837935

838936
def _element(
839937
html_element: str,

pandas/io/formats/templates/html_table.tpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
{% if exclude_styles %}
2222
{% for c in r %}
2323
{% if c.is_visible != False %}
24-
<{{c.type}} {{c.attributes}}>{{c.value}}</{{c.type}}>
24+
<{{c.type}} {{c.attributes}}>{{c.display_value}}</{{c.type}}>
2525
{% endif %}
2626
{% endfor %}
2727
{% else %}
2828
{% for c in r %}
2929
{% if c.is_visible != False %}
30-
<{{c.type}} {%- if c.id is defined %} id="T_{{uuid}}_{{c.id}}" {%- endif %} class="{{c.class}}" {{c.attributes}}>{{c.value}}</{{c.type}}>
30+
<{{c.type}} {%- if c.id is defined %} id="T_{{uuid}}_{{c.id}}" {%- endif %} class="{{c.class}}" {{c.attributes}}>{{c.display_value}}</{{c.type}}>
3131
{% endif %}
3232
{% endfor %}
3333
{% endif %}

0 commit comments

Comments
 (0)