diff --git a/ci/code_checks.sh b/ci/code_checks.sh index 989ef6958d2e6..7dd347327f3cc 100755 --- a/ci/code_checks.sh +++ b/ci/code_checks.sh @@ -88,7 +88,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then pandas.errors.UnsupportedFunctionCall \ pandas.test \ pandas.NaT \ - pandas.io.formats.style.Styler.to_html \ pandas.read_feather \ pandas.DataFrame.to_feather \ pandas.read_parquet \ @@ -112,15 +111,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then pandas.DatetimeIndex.snap \ pandas.api.indexers.BaseIndexer \ pandas.api.indexers.VariableOffsetWindowIndexer \ - pandas.io.formats.style.Styler.set_caption \ - pandas.io.formats.style.Styler.set_sticky \ - pandas.io.formats.style.Styler.set_uuid \ - pandas.io.formats.style.Styler.clear \ - pandas.io.formats.style.Styler.highlight_null \ - pandas.io.formats.style.Styler.highlight_max \ - pandas.io.formats.style.Styler.highlight_min \ - pandas.io.formats.style.Styler.bar \ - pandas.io.formats.style.Styler.to_string \ pandas.api.extensions.ExtensionDtype \ pandas.api.extensions.ExtensionArray \ pandas.arrays.PandasArray \ diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 278527fa8fc85..a45ea881d8dad 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -1307,6 +1307,21 @@ def to_html( See Also -------- DataFrame.to_html: Write a DataFrame to a file, buffer or string in HTML format. + + Examples + -------- + >>> df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]}) + >>> print(df.style.to_html()) # doctest: +SKIP + + + + + + + + + ... """ obj = self._copy(deepcopy=True) # manipulate table_styles on obj, not self @@ -1419,6 +1434,12 @@ def to_string( ------- str or None If `buf` is None, returns the result as a string. Otherwise returns `None`. + + Examples + -------- + >>> df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]}) + >>> df.style.to_string() + ' A B\\n0 1 3\\n1 2 4\\n' """ obj = self._copy(deepcopy=True) @@ -1650,6 +1671,21 @@ def clear(self) -> None: Reset the ``Styler``, removing any previously applied styles. Returns None. + + Examples + -------- + >>> df = pd.DataFrame({'A': [1, 2], 'B': [3, np.nan]}) + + After any added style: + + >>> df.style.highlight_null(color='yellow') # doctest: +SKIP + + Remove it with: + + >>> df.style.clear() # doctest: +SKIP + + Please see: + `Table Visualization <../../user_guide/style.ipynb>`_ for more examples. """ # create default GH 40675 clean_copy = Styler(self.data, uuid=self.uuid) @@ -2257,6 +2293,22 @@ def set_uuid(self, uuid: str) -> Styler: Almost all HTML elements within the table, and including the ``
 AB
`` element are assigned ``id`` attributes. The format is ``T_uuid_`` where ```` is typically a more specific identifier, such as ``row1_col2``. + + Examples + -------- + >>> df = pd.DataFrame([[1, 2], [3, 4]], index=['A', 'B'], columns=['c1', 'c2']) + + You can get the `id` attributes with the following: + + >>> print((df).style.to_html()) # doctest: +SKIP + + To add a title to column `c1`, its `id` is T_20a7d_level0_col0: + + >>> df.style.set_uuid("T_20a7d_level0_col0") + ... .set_caption("Test") # doctest: +SKIP + + Please see: + `Table visualization <../../user_guide/style.ipynb>`_ for more examples. """ self.uuid = uuid return self @@ -2275,6 +2327,14 @@ def set_caption(self, caption: str | tuple | list) -> Styler: Returns ------- Styler + + Examples + -------- + >>> df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]}) + >>> df.style.set_caption("test") # doctest: +SKIP + + Please see: + `Table Visualization <../../user_guide/style.ipynb>`_ for more examples. """ msg = "`caption` must be either a string or 2-tuple of strings." if isinstance(caption, (list, tuple)): @@ -2323,6 +2383,14 @@ def set_sticky( - `styler.set_sticky(axis="columns").hide(axis="columns")` may produce strange behaviour due to CSS controls with missing elements. + + Examples + -------- + >>> df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]}) + >>> df.style.set_sticky(axis="index") # doctest: +SKIP + + Please see: + `Table Visualization <../../user_guide/style.ipynb>`_ for more examples. """ axis = self.data._get_axis_number(axis) obj = self.data.index if axis == 0 else self.data.columns @@ -3073,6 +3141,11 @@ def bar( # pylint: disable=disallowed-name This section of the user guide: `Table Visualization <../../user_guide/style.ipynb>`_ gives a number of examples for different settings and color coordination. + + Examples + -------- + >>> df = pd.DataFrame({'A': [1, 2, 3, 4], 'B': [3, 4, 5, 6]}) + >>> df.style.bar(subset=['A'], color='gray') # doctest: +SKIP """ if color is None and cmap is None: color = "#d65f5f" @@ -3147,6 +3220,14 @@ def highlight_null( Styler.highlight_min: Highlight the minimum with a style. Styler.highlight_between: Highlight a defined range with a style. Styler.highlight_quantile: Highlight values defined by a quantile with a style. + + Examples + -------- + >>> df = pd.DataFrame({'A': [1, 2], 'B': [3, np.nan]}) + >>> df.style.highlight_null(color='yellow') # doctest: +SKIP + + Please see: + `Table Visualization <../../user_guide/style.ipynb>`_ for more examples. """ def f(data: DataFrame, props: str) -> np.ndarray: @@ -3193,6 +3274,14 @@ def highlight_max( Styler.highlight_min: Highlight the minimum with a style. Styler.highlight_between: Highlight a defined range with a style. Styler.highlight_quantile: Highlight values defined by a quantile with a style. + + Examples + -------- + >>> df = pd.DataFrame({'A': [2, 1], 'B': [3, 4]}) + >>> df.style.highlight_max(color='yellow') # doctest: +SKIP + + Please see: + `Table Visualization <../../user_guide/style.ipynb>`_ for more examples. """ if props is None: @@ -3241,6 +3330,14 @@ def highlight_min( Styler.highlight_max: Highlight the maximum with a style. Styler.highlight_between: Highlight a defined range with a style. Styler.highlight_quantile: Highlight values defined by a quantile with a style. + + Examples + -------- + >>> df = pd.DataFrame({'A': [2, 1], 'B': [3, 4]}) + >>> df.style.highlight_min(color='yellow') # doctest: +SKIP + + Please see: + `Table Visualization <../../user_guide/style.ipynb>`_ for more examples. """ if props is None: