Skip to content

Commit

Permalink
DOC: Ex03 errors docstrings (pandas-dev#56905)
Browse files Browse the repository at this point in the history
* Fix exo3 and remove unused code

* Remove unused in code_checks

* Fix 's' to 'v' in Styler.map_index() method

* Try set 's' to {var}

* Use `label` for more readable variable name

* Fix merge mistake

* Fix merge error

---------

Co-authored-by: Marc Garcia <[email protected]>
  • Loading branch information
luke396 and datapythonista authored Jan 18, 2024
1 parent d4bd7e4 commit f459437
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 44 deletions.
4 changes: 0 additions & 4 deletions ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
pandas.io.formats.style.Styler.to_latex \
pandas.read_parquet \
pandas.DataFrame.to_sql \
pandas.io.formats.style.Styler.map \
pandas.io.formats.style.Styler.apply_index \
pandas.io.formats.style.Styler.map_index \
pandas.io.formats.style.Styler.format \
RET=$(($RET + $?)) ; echo $MSG "DONE"

fi
Expand Down
33 changes: 16 additions & 17 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -1880,9 +1880,9 @@ def _apply_index(
func="take a Series and return a string array of the same length",
input_note="the index as a Series, if an Index, or a level of a MultiIndex",
output_note="an identically sized array of CSS styles as strings",
var="s",
ret='np.where(s == "B", "background-color: yellow;", "")',
ret2='["background-color: yellow;" if "x" in v else "" for v in s]',
var="label",
ret='np.where(label == "B", "background-color: yellow;", "")',
ret2='["background-color: yellow;" if "x" in v else "" for v in label]',
)
def apply_index(
self,
Expand Down Expand Up @@ -1932,8 +1932,8 @@ def apply_index(
--------
Basic usage to conditionally highlight values in the index.
>>> df = pd.DataFrame([[1,2], [3,4]], index=["A", "B"])
>>> def color_b(s):
>>> df = pd.DataFrame([[1, 2], [3, 4]], index=["A", "B"])
>>> def color_b({var}):
... return {ret}
>>> df.style.{this}_index(color_b) # doctest: +SKIP
Expand All @@ -1945,8 +1945,8 @@ def apply_index(
>>> df = pd.DataFrame([np.arange(8)], columns=midx)
>>> def highlight_x({var}):
... return {ret2}
>>> df.style.{this}_index(highlight_x, axis="columns", level=[0, 2])
... # doctest: +SKIP
>>> df.style.{this}_index(
... highlight_x, axis="columns", level=[0, 2]) # doctest: +SKIP
.. figure:: ../../_static/style/appmaphead2.png
"""
Expand All @@ -1968,9 +1968,9 @@ def apply_index(
func="take a scalar and return a string",
input_note="an index value, if an Index, or a level value of a MultiIndex",
output_note="CSS styles as a string",
var="v",
ret='"background-color: yellow;" if v == "B" else None',
ret2='"background-color: yellow;" if "x" in v else None',
var="label",
ret='"background-color: yellow;" if label == "B" else None',
ret2='"background-color: yellow;" if "x" in label else None',
)
def map_index(
self,
Expand Down Expand Up @@ -2073,17 +2073,16 @@ def map(self, func: Callable, subset: Subset | None = None, **kwargs) -> Styler:
Using ``subset`` to restrict application to a single column or multiple columns
>>> df.style.map(color_negative, color='red', subset="A")
... # doctest: +SKIP
>>> df.style.map(color_negative, color='red', subset=["A", "B"])
... # doctest: +SKIP
>>> df.style.map(color_negative, color='red', subset="A") # doctest: +SKIP
>>> df.style.map(color_negative,
... color='red', subset=["A", "B"]) # doctest: +SKIP
Using a 2d input to ``subset`` to select rows in addition to columns
>>> df.style.map(color_negative, color='red',
... subset=([0,1,2], slice(None))) # doctest: +SKIP
>>> df.style.map(color_negative, color='red', subset=(slice(0,5,2), "A"))
... # doctest: +SKIP
... subset=([0, 1, 2], slice(None))) # doctest: +SKIP
>>> df.style.map(color_negative,
... color='red', subset=(slice(0, 5, 2), "A")) # doctest: +SKIP
See `Table Visualization <../../user_guide/style.ipynb>`_ user guide for
more details.
Expand Down
45 changes: 22 additions & 23 deletions pandas/io/formats/style_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -1063,33 +1063,33 @@ def format(
Using a ``formatter`` specification on consistent column dtypes
>>> df.style.format('{:.2f}', na_rep='MISS', subset=[0,1]) # doctest: +SKIP
>>> df.style.format('{:.2f}', na_rep='MISS', subset=[0, 1]) # doctest: +SKIP
0 1 2
0 MISS 1.00 A
1 2.00 MISS 3.000000
Using the default ``formatter`` for unspecified columns
>>> df.style.format({0: '{:.2f}', 1: '£ {:.1f}'}, na_rep='MISS', precision=1)
... # doctest: +SKIP
>>> df.style.format({0: '{:.2f}', 1: '£ {:.1f}'},
... na_rep='MISS', precision=1) # doctest: +SKIP
0 1 2
0 MISS £ 1.0 A
1 2.00 MISS 3.0
Multiple ``na_rep`` or ``precision`` specifications under the default
``formatter``.
>>> (df.style.format(na_rep='MISS', precision=1, subset=[0])
... .format(na_rep='PASS', precision=2, subset=[1, 2])) # doctest: +SKIP
>>> (df.style.format(na_rep='MISS', precision=1, subset=[0]).format(
... na_rep='PASS', precision=2, subset=[1, 2])) # doctest: +SKIP
0 1 2
0 MISS 1.00 A
1 2.0 PASS 3.00
Using a callable ``formatter`` function.
>>> func = lambda s: 'STRING' if isinstance(s, str) else 'FLOAT'
>>> df.style.format({0: '{:.1f}', 2: func}, precision=4, na_rep='MISS')
... # doctest: +SKIP
>>> df.style.format({0: '{:.1f}', 2: func},
... precision=4, na_rep='MISS') # doctest: +SKIP
0 1 2
0 MISS 1.0000 STRING
1 2.0 MISS FLOAT
Expand All @@ -1098,8 +1098,7 @@ def format(
>>> df = pd.DataFrame([['<div></div>', '"A&B"', None]])
>>> s = df.style.format(
... '<a href="a.com/{0}">{0}</a>', escape="html", na_rep="NA"
... )
... '<a href="a.com/{0}">{0}</a>', escape="html", na_rep="NA")
>>> s.to_html() # doctest: +SKIP
...
<td .. ><a href="a.com/&lt;div&gt;&lt;/div&gt;">&lt;div&gt;&lt;/div&gt;</a></td>
Expand All @@ -1110,8 +1109,8 @@ def format(
Using a ``formatter`` with ``escape`` in 'latex' mode.
>>> df = pd.DataFrame([["123"], ["~ ^"], ["$%#"]])
>>> df.style.format("\\textbf{{{}}}", escape="latex").to_latex()
... # doctest: +SKIP
>>> df.style.format("\\textbf{{{}}}",
... escape="latex").to_latex() # doctest: +SKIP
\begin{tabular}{ll}
& 0 \\
0 & \textbf{123} \\
Expand All @@ -1122,10 +1121,10 @@ def format(
Applying ``escape`` in 'latex-math' mode. In the example below
we enter math mode using the character ``$``.
>>> df = pd.DataFrame([[r"$\sum_{i=1}^{10} a_i$ a~b $\alpha \
... = \frac{\beta}{\zeta^2}$"], ["%#^ $ \$x^2 $"]])
>>> df.style.format(escape="latex-math").to_latex()
... # doctest: +SKIP
>>> df = pd.DataFrame([
... [r"$\sum_{i=1}^{10} a_i$ a~b $\alpha = \frac{\beta}{\zeta^2}$"],
... [r"%#^ $ \$x^2 $"]])
>>> df.style.format(escape="latex-math").to_latex() # doctest: +SKIP
\begin{tabular}{ll}
& 0 \\
0 & $\sum_{i=1}^{10} a_i$ a\textasciitilde b $\alpha = \frac{\beta}{\zeta^2}$ \\
Expand All @@ -1135,10 +1134,10 @@ def format(
We can use the character ``\(`` to enter math mode and the character ``\)``
to close math mode.
>>> df = pd.DataFrame([[r"\(\sum_{i=1}^{10} a_i\) a~b \(\alpha \
... = \frac{\beta}{\zeta^2}\)"], ["%#^ \( \$x^2 \)"]])
>>> df.style.format(escape="latex-math").to_latex()
... # doctest: +SKIP
>>> df = pd.DataFrame([
... [r"\(\sum_{i=1}^{10} a_i\) a~b \(\alpha = \frac{\beta}{\zeta^2}\)"],
... [r"%#^ \( \$x^2 \)"]])
>>> df.style.format(escape="latex-math").to_latex() # doctest: +SKIP
\begin{tabular}{ll}
& 0 \\
0 & \(\sum_{i=1}^{10} a_i\) a\textasciitilde b \(\alpha
Expand All @@ -1149,10 +1148,10 @@ def format(
If we have in one DataFrame cell a combination of both shorthands
for math formulas, the shorthand with the sign ``$`` will be applied.
>>> df = pd.DataFrame([[r"\( x^2 \) $x^2$"], \
>>> df = pd.DataFrame([
... [r"\( x^2 \) $x^2$"],
... [r"$\frac{\beta}{\zeta}$ \(\frac{\beta}{\zeta}\)"]])
>>> df.style.format(escape="latex-math").to_latex()
... # doctest: +SKIP
>>> df.style.format(escape="latex-math").to_latex() # doctest: +SKIP
\begin{tabular}{ll}
& 0 \\
0 & \textbackslash ( x\textasciicircum 2 \textbackslash ) $x^2$ \\
Expand All @@ -1169,7 +1168,7 @@ def format(
>>> df = pd.DataFrame({"A": [1, 0, -1]})
>>> pseudo_css = "number-format: 0§[Red](0)§-§@;"
>>> filename = "formatted_file.xlsx"
>>> df.style.map(lambda v: pseudo_css).to_excel(filename) # doctest: +SKIP
>>> df.style.map(lambda v: pseudo_css).to_excel(filename) # doctest: +SKIP
.. figure:: ../../_static/style/format_excel_css.png
"""
Expand Down

0 comments on commit f459437

Please sign in to comment.