Skip to content

Commit d3f9207

Browse files
attack68JulianWgs
authored andcommitted
DOC: Styler docs - split PR from pandas-dev#39720 (pandas-dev#40493)
1 parent d4e9a87 commit d3f9207

File tree

1 file changed

+99
-49
lines changed

1 file changed

+99
-49
lines changed

pandas/io/formats/style.py

+99-49
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,10 @@ def set_tooltips(
217217
css_class: Optional[str] = None,
218218
) -> Styler:
219219
"""
220-
Add string based tooltips that will appear in the `Styler` HTML result. These
221-
tooltips are applicable only to`<td>` elements.
220+
Set the DataFrame of strings on ``Styler`` generating ``:hover`` tooltips.
221+
222+
These string based tooltips are only applicable to ``<td>`` HTML elements,
223+
and cannot be used for column or index headers.
222224
223225
.. versionadded:: 1.3.0
224226
@@ -227,7 +229,7 @@ def set_tooltips(
227229
ttips : DataFrame
228230
DataFrame containing strings that will be translated to tooltips, mapped
229231
by identical column and index values that must exist on the underlying
230-
`Styler` data. None, NaN values, and empty strings will be ignored and
232+
Styler data. None, NaN values, and empty strings will be ignored and
231233
not affect the rendered HTML.
232234
props : list-like or str, optional
233235
List of (attr, value) tuples or a valid CSS string. If ``None`` adopts
@@ -671,21 +673,33 @@ def format(
671673

672674
def set_td_classes(self, classes: DataFrame) -> Styler:
673675
"""
674-
Add string based CSS class names to data cells that will appear within the
675-
`Styler` HTML result. These classes are added within specified `<td>` elements.
676+
Set the DataFrame of strings added to the ``class`` attribute of ``<td>``
677+
HTML elements.
676678
677679
Parameters
678680
----------
679681
classes : DataFrame
680682
DataFrame containing strings that will be translated to CSS classes,
681-
mapped by identical column and index values that must exist on the
682-
underlying `Styler` data. None, NaN values, and empty strings will
683+
mapped by identical column and index key values that must exist on the
684+
underlying Styler data. None, NaN values, and empty strings will
683685
be ignored and not affect the rendered HTML.
684686
685687
Returns
686688
-------
687689
self : Styler
688690
691+
See Also
692+
--------
693+
Styler.set_table_styles: Set the table styles included within the ``<style>``
694+
HTML element.
695+
Styler.set_table_attributes: Set the table attributes added to the ``<table>``
696+
HTML element.
697+
698+
Notes
699+
-----
700+
Can be used in combination with ``Styler.set_table_styles`` to define an
701+
internal CSS solution without reference to external CSS files.
702+
689703
Examples
690704
--------
691705
>>> df = pd.DataFrame(data=[[1, 2, 3], [4, 5, 6]], columns=["A", "B", "C"])
@@ -707,16 +721,16 @@ def set_td_classes(self, classes: DataFrame) -> Styler:
707721
Form of the output with new additional css classes,
708722
709723
>>> df = pd.DataFrame([[1]])
710-
>>> css = pd.DataFrame(["other-class"])
724+
>>> css = pd.DataFrame([["other-class"]])
711725
>>> s = Styler(df, uuid="_", cell_ids=False).set_td_classes(css)
712726
>>> s.hide_index().render()
713-
'<style type="text/css" ></style>'
714-
'<table id="T__" >'
727+
'<style type="text/css"></style>'
728+
'<table id="T__">'
715729
' <thead>'
716730
' <tr><th class="col_heading level0 col0" >0</th></tr>'
717731
' </thead>'
718732
' <tbody>'
719-
' <tr><td class="data row0 col0 other-class" >1</td></tr>'
733+
' <tr><td class="data row0 col0 other-class" >1</td></tr>'
720734
' </tbody>'
721735
'</table>'
722736
"""
@@ -736,7 +750,7 @@ def set_td_classes(self, classes: DataFrame) -> Styler:
736750

737751
def render(self, **kwargs) -> str:
738752
"""
739-
Render the built up styles to HTML.
753+
Render the ``Styler`` including all applied styles to HTML.
740754
741755
Parameters
742756
----------
@@ -753,7 +767,7 @@ def render(self, **kwargs) -> str:
753767
754768
Notes
755769
-----
756-
``Styler`` objects have defined the ``_repr_html_`` method
770+
Styler objects have defined the ``_repr_html_`` method
757771
which automatically calls ``self.render()`` when it's the
758772
last item in a Notebook cell. When calling ``Styler.render()``
759773
directly, wrap the result in ``IPython.display.HTML`` to view
@@ -779,7 +793,7 @@ def render(self, **kwargs) -> str:
779793

780794
def _update_ctx(self, attrs: DataFrame) -> None:
781795
"""
782-
Update the state of the Styler for data cells.
796+
Update the state of the ``Styler`` for data cells.
783797
784798
Collects a mapping of {index_label: [('<property>', '<value>'), ..]}.
785799
@@ -839,7 +853,7 @@ def __deepcopy__(self, memo) -> Styler:
839853

840854
def clear(self) -> None:
841855
"""
842-
Reset the styler, removing any previously applied styles.
856+
Reset the ``Styler``, removing any previously applied styles.
843857
844858
Returns None.
845859
"""
@@ -923,10 +937,11 @@ def apply(
923937
Parameters
924938
----------
925939
func : function
926-
``func`` should take a Series or DataFrame (depending
927-
on ``axis``), and return an object with the same shape.
928-
Must return a DataFrame with identical index and
929-
column labels or an ndarray with same shape as input when ``axis=None``.
940+
``func`` should take a Series if ``axis`` in [0,1] and return an object
941+
of same length, also with identical index if the object is a Series.
942+
``func`` should take a DataFrame if ``axis`` is ``None`` and return either
943+
an ndarray with the same shape or a DataFrame with identical columns and
944+
index.
930945
931946
.. versionchanged:: 1.3.0
932947
@@ -944,13 +959,16 @@ def apply(
944959
-------
945960
self : Styler
946961
962+
See Also
963+
--------
964+
Styler.where: Apply CSS-styles based on a conditional function elementwise.
965+
Styler.applymap: Apply a CSS-styling function elementwise.
966+
947967
Notes
948968
-----
949-
The output of ``func`` should be elements having CSS style as string or,
969+
The elements of the output of ``func`` should be CSS styles as strings, in the
970+
format 'attribute: value; attribute2: value2; ...' or,
950971
if nothing is to be applied to that element, an empty string or ``None``.
951-
The output shape must match the input, i.e. if
952-
``x`` is the input row, column, or table (depending on ``axis``),
953-
then ``func(x).shape == x.shape`` should be ``True``.
954972
955973
This is similar to ``DataFrame.apply``, except that ``axis=None``
956974
applies the function to the entire DataFrame at once,
@@ -1001,13 +1019,14 @@ def applymap(self, func: Callable, subset=None, **kwargs) -> Styler:
10011019
10021020
See Also
10031021
--------
1004-
Styler.where: Updates the HTML representation with a style which is
1005-
selected in accordance with the return value of a function.
1022+
Styler.where: Apply CSS-styles based on a conditional function elementwise.
1023+
Styler.apply: Apply a CSS-styling function column-wise, row-wise, or table-wise.
10061024
10071025
Notes
10081026
-----
1009-
The output of ``func`` should be a CSS style as string or, if nothing is to be
1010-
applied, an empty string or ``None``.
1027+
The elements of the output of ``func`` should be CSS styles as strings, in the
1028+
format 'attribute: value; attribute2: value2; ...' or,
1029+
if nothing is to be applied to that element, an empty string or ``None``.
10111030
10121031
Examples
10131032
--------
@@ -1030,7 +1049,7 @@ def where(
10301049
**kwargs,
10311050
) -> Styler:
10321051
"""
1033-
Apply a function elementwise.
1052+
Apply CSS-styles based on a conditional function elementwise.
10341053
10351054
Updates the HTML representation with a style which is
10361055
selected in accordance with the return value of a function.
@@ -1055,7 +1074,15 @@ def where(
10551074
10561075
See Also
10571076
--------
1058-
Styler.applymap: Updates the HTML representation with the result.
1077+
Styler.applymap: Apply a CSS-styling function elementwise.
1078+
Styler.apply: Apply a CSS-styling function column-wise, row-wise, or table-wise.
1079+
1080+
Examples
1081+
--------
1082+
>>> def cond(v):
1083+
... return v > 1 and v != 4
1084+
>>> df = pd.DataFrame([[1, 2], [3, 4]])
1085+
>>> df.style.where(cond, value='color:red;', other='font-size:2em;')
10591086
"""
10601087
if other is None:
10611088
other = ""
@@ -1092,10 +1119,9 @@ def set_precision(self, precision: int) -> Styler:
10921119

10931120
def set_table_attributes(self, attributes: str) -> Styler:
10941121
"""
1095-
Set the table attributes.
1122+
Set the table attributes added to the ``<table>`` HTML element.
10961123
1097-
These are the items that show up in the opening ``<table>`` tag
1098-
in addition to automatic (by default) id.
1124+
These are items in addition to automatic (by default) ``id`` attribute.
10991125
11001126
Parameters
11011127
----------
@@ -1105,6 +1131,13 @@ def set_table_attributes(self, attributes: str) -> Styler:
11051131
-------
11061132
self : Styler
11071133
1134+
See Also
1135+
--------
1136+
Styler.set_table_styles: Set the table styles included within the ``<style>``
1137+
HTML element.
1138+
Styler.set_td_classes: Set the DataFrame of strings added to the ``class``
1139+
attribute of ``<td>`` HTML elements.
1140+
11081141
Examples
11091142
--------
11101143
>>> df = pd.DataFrame(np.random.randn(10, 4))
@@ -1116,23 +1149,23 @@ def set_table_attributes(self, attributes: str) -> Styler:
11161149

11171150
def export(self) -> List[Tuple[Callable, Tuple, Dict]]:
11181151
"""
1119-
Export the styles to applied to the current Styler.
1152+
Export the styles applied to the current ``Styler``.
11201153
1121-
Can be applied to a second style with ``Styler.use``.
1154+
Can be applied to a second Styler with ``Styler.use``.
11221155
11231156
Returns
11241157
-------
11251158
styles : list
11261159
11271160
See Also
11281161
--------
1129-
Styler.use: Set the styles on the current Styler.
1162+
Styler.use: Set the styles on the current ``Styler``.
11301163
"""
11311164
return self._todo
11321165

11331166
def use(self, styles: List[Tuple[Callable, Tuple, Dict]]) -> Styler:
11341167
"""
1135-
Set the styles on the current Styler.
1168+
Set the styles on the current ``Styler``.
11361169
11371170
Possibly uses styles from ``Styler.export``.
11381171
@@ -1147,14 +1180,14 @@ def use(self, styles: List[Tuple[Callable, Tuple, Dict]]) -> Styler:
11471180
11481181
See Also
11491182
--------
1150-
Styler.export : Export the styles to applied to the current Styler.
1183+
Styler.export : Export the styles to applied to the current ``Styler``.
11511184
"""
11521185
self._todo.extend(styles)
11531186
return self
11541187

11551188
def set_uuid(self, uuid: str) -> Styler:
11561189
"""
1157-
Set the uuid for a Styler.
1190+
Set the uuid applied to ``id`` attributes of HTML elements.
11581191
11591192
Parameters
11601193
----------
@@ -1163,13 +1196,19 @@ def set_uuid(self, uuid: str) -> Styler:
11631196
Returns
11641197
-------
11651198
self : Styler
1199+
1200+
Notes
1201+
-----
1202+
Almost all HTML elements within the table, and including the ``<table>`` element
1203+
are assigned ``id`` attributes. The format is ``T_uuid_<extra>`` where
1204+
``<extra>`` is typically a more specific identifier, such as ``row1_col2``.
11661205
"""
11671206
self.uuid = uuid
11681207
return self
11691208

11701209
def set_caption(self, caption: str) -> Styler:
11711210
"""
1172-
Set the caption on a Styler.
1211+
Set the text added to a ``<caption>`` HTML element.
11731212
11741213
Parameters
11751214
----------
@@ -1189,9 +1228,7 @@ def set_table_styles(
11891228
overwrite: bool = True,
11901229
) -> Styler:
11911230
"""
1192-
Set the table styles on a Styler.
1193-
1194-
These are placed in a ``<style>`` tag before the generated HTML table.
1231+
Set the table styles included within the ``<style>`` HTML element.
11951232
11961233
This function can be used to style the entire table, columns, rows or
11971234
specific HTML selectors.
@@ -1232,6 +1269,13 @@ def set_table_styles(
12321269
-------
12331270
self : Styler
12341271
1272+
See Also
1273+
--------
1274+
Styler.set_td_classes: Set the DataFrame of strings added to the ``class``
1275+
attribute of ``<td>`` HTML elements.
1276+
Styler.set_table_attributes: Set the table attributes added to the ``<table>``
1277+
HTML element.
1278+
12351279
Examples
12361280
--------
12371281
>>> df = pd.DataFrame(np.random.randn(10, 4),
@@ -1295,7 +1339,7 @@ def set_table_styles(
12951339

12961340
def set_na_rep(self, na_rep: str) -> Styler:
12971341
"""
1298-
Set the missing data representation on a Styler.
1342+
Set the missing data representation on a ``Styler``.
12991343
13001344
.. versionadded:: 1.0.0
13011345
@@ -1505,7 +1549,8 @@ def css(rgba) -> str:
15051549

15061550
def set_properties(self, subset=None, **kwargs) -> Styler:
15071551
"""
1508-
Method to set one or more non-data dependent properties or each cell.
1552+
Set defined CSS-properties to each ``<td>`` HTML element within the given
1553+
subset.
15091554
15101555
Parameters
15111556
----------
@@ -1518,6 +1563,11 @@ def set_properties(self, subset=None, **kwargs) -> Styler:
15181563
-------
15191564
self : Styler
15201565
1566+
Notes
1567+
-----
1568+
This is a convenience methods which wraps the :meth:`Styler.applymap` calling a
1569+
function returning the CSS-properties independently of the data.
1570+
15211571
Examples
15221572
--------
15231573
>>> df = pd.DataFrame(np.random.randn(10, 4))
@@ -1865,8 +1915,8 @@ def pipe(self, func: Callable, *args, **kwargs):
18651915
See Also
18661916
--------
18671917
DataFrame.pipe : Analogous method for DataFrame.
1868-
Styler.apply : Apply a function row-wise, column-wise, or table-wise to
1869-
modify the dataframe's styling.
1918+
Styler.apply : Apply a CSS-styling function column-wise, row-wise, or
1919+
table-wise.
18701920
18711921
Notes
18721922
-----
@@ -1915,7 +1965,7 @@ def pipe(self, func: Callable, *args, **kwargs):
19151965
class _Tooltips:
19161966
"""
19171967
An extension to ``Styler`` that allows for and manipulates tooltips on hover
1918-
of table data-cells in the HTML result.
1968+
of ``<td>`` cells in the HTML result.
19191969
19201970
Parameters
19211971
----------
@@ -1924,7 +1974,7 @@ class _Tooltips:
19241974
css_props: list-like, default; see Notes
19251975
List of (attr, value) tuples defining properties of the CSS class.
19261976
tooltips: DataFrame, default empty
1927-
DataFrame of strings aligned with underlying ``Styler`` data for tooltip
1977+
DataFrame of strings aligned with underlying Styler data for tooltip
19281978
display.
19291979
19301980
Notes
@@ -2025,7 +2075,7 @@ def _translate(self, styler_data: FrameOrSeriesUnion, uuid: str, d: Dict):
20252075
"""
20262076
Mutate the render dictionary to allow for tooltips:
20272077
2028-
- Add `<span>` HTML element to each data cells `display_value`. Ignores
2078+
- Add ``<span>`` HTML element to each data cells ``display_value``. Ignores
20292079
headers.
20302080
- Add table level CSS styles to control pseudo classes.
20312081

0 commit comments

Comments
 (0)