Skip to content

Commit f459437

Browse files
DOC: Ex03 errors docstrings (#56905)
* 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]>
1 parent d4bd7e4 commit f459437

File tree

3 files changed

+38
-44
lines changed

3 files changed

+38
-44
lines changed

ci/code_checks.sh

-4
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
8080
pandas.io.formats.style.Styler.to_latex \
8181
pandas.read_parquet \
8282
pandas.DataFrame.to_sql \
83-
pandas.io.formats.style.Styler.map \
84-
pandas.io.formats.style.Styler.apply_index \
85-
pandas.io.formats.style.Styler.map_index \
86-
pandas.io.formats.style.Styler.format \
8783
RET=$(($RET + $?)) ; echo $MSG "DONE"
8884

8985
fi

pandas/io/formats/style.py

+16-17
Original file line numberDiff line numberDiff line change
@@ -1880,9 +1880,9 @@ def _apply_index(
18801880
func="take a Series and return a string array of the same length",
18811881
input_note="the index as a Series, if an Index, or a level of a MultiIndex",
18821882
output_note="an identically sized array of CSS styles as strings",
1883-
var="s",
1884-
ret='np.where(s == "B", "background-color: yellow;", "")',
1885-
ret2='["background-color: yellow;" if "x" in v else "" for v in s]',
1883+
var="label",
1884+
ret='np.where(label == "B", "background-color: yellow;", "")',
1885+
ret2='["background-color: yellow;" if "x" in v else "" for v in label]',
18861886
)
18871887
def apply_index(
18881888
self,
@@ -1932,8 +1932,8 @@ def apply_index(
19321932
--------
19331933
Basic usage to conditionally highlight values in the index.
19341934
1935-
>>> df = pd.DataFrame([[1,2], [3,4]], index=["A", "B"])
1936-
>>> def color_b(s):
1935+
>>> df = pd.DataFrame([[1, 2], [3, 4]], index=["A", "B"])
1936+
>>> def color_b({var}):
19371937
... return {ret}
19381938
>>> df.style.{this}_index(color_b) # doctest: +SKIP
19391939
@@ -1945,8 +1945,8 @@ def apply_index(
19451945
>>> df = pd.DataFrame([np.arange(8)], columns=midx)
19461946
>>> def highlight_x({var}):
19471947
... return {ret2}
1948-
>>> df.style.{this}_index(highlight_x, axis="columns", level=[0, 2])
1949-
... # doctest: +SKIP
1948+
>>> df.style.{this}_index(
1949+
... highlight_x, axis="columns", level=[0, 2]) # doctest: +SKIP
19501950
19511951
.. figure:: ../../_static/style/appmaphead2.png
19521952
"""
@@ -1968,9 +1968,9 @@ def apply_index(
19681968
func="take a scalar and return a string",
19691969
input_note="an index value, if an Index, or a level value of a MultiIndex",
19701970
output_note="CSS styles as a string",
1971-
var="v",
1972-
ret='"background-color: yellow;" if v == "B" else None',
1973-
ret2='"background-color: yellow;" if "x" in v else None',
1971+
var="label",
1972+
ret='"background-color: yellow;" if label == "B" else None',
1973+
ret2='"background-color: yellow;" if "x" in label else None',
19741974
)
19751975
def map_index(
19761976
self,
@@ -2073,17 +2073,16 @@ def map(self, func: Callable, subset: Subset | None = None, **kwargs) -> Styler:
20732073
20742074
Using ``subset`` to restrict application to a single column or multiple columns
20752075
2076-
>>> df.style.map(color_negative, color='red', subset="A")
2077-
... # doctest: +SKIP
2078-
>>> df.style.map(color_negative, color='red', subset=["A", "B"])
2079-
... # doctest: +SKIP
2076+
>>> df.style.map(color_negative, color='red', subset="A") # doctest: +SKIP
2077+
>>> df.style.map(color_negative,
2078+
... color='red', subset=["A", "B"]) # doctest: +SKIP
20802079
20812080
Using a 2d input to ``subset`` to select rows in addition to columns
20822081
20832082
>>> df.style.map(color_negative, color='red',
2084-
... subset=([0,1,2], slice(None))) # doctest: +SKIP
2085-
>>> df.style.map(color_negative, color='red', subset=(slice(0,5,2), "A"))
2086-
... # doctest: +SKIP
2083+
... subset=([0, 1, 2], slice(None))) # doctest: +SKIP
2084+
>>> df.style.map(color_negative,
2085+
... color='red', subset=(slice(0, 5, 2), "A")) # doctest: +SKIP
20872086
20882087
See `Table Visualization <../../user_guide/style.ipynb>`_ user guide for
20892088
more details.

pandas/io/formats/style_render.py

+22-23
Original file line numberDiff line numberDiff line change
@@ -1063,33 +1063,33 @@ def format(
10631063
10641064
Using a ``formatter`` specification on consistent column dtypes
10651065
1066-
>>> df.style.format('{:.2f}', na_rep='MISS', subset=[0,1]) # doctest: +SKIP
1066+
>>> df.style.format('{:.2f}', na_rep='MISS', subset=[0, 1]) # doctest: +SKIP
10671067
0 1 2
10681068
0 MISS 1.00 A
10691069
1 2.00 MISS 3.000000
10701070
10711071
Using the default ``formatter`` for unspecified columns
10721072
1073-
>>> df.style.format({0: '{:.2f}', 1: '£ {:.1f}'}, na_rep='MISS', precision=1)
1074-
... # doctest: +SKIP
1073+
>>> df.style.format({0: '{:.2f}', 1: '£ {:.1f}'},
1074+
... na_rep='MISS', precision=1) # doctest: +SKIP
10751075
0 1 2
10761076
0 MISS £ 1.0 A
10771077
1 2.00 MISS 3.0
10781078
10791079
Multiple ``na_rep`` or ``precision`` specifications under the default
10801080
``formatter``.
10811081
1082-
>>> (df.style.format(na_rep='MISS', precision=1, subset=[0])
1083-
... .format(na_rep='PASS', precision=2, subset=[1, 2])) # doctest: +SKIP
1082+
>>> (df.style.format(na_rep='MISS', precision=1, subset=[0]).format(
1083+
... na_rep='PASS', precision=2, subset=[1, 2])) # doctest: +SKIP
10841084
0 1 2
10851085
0 MISS 1.00 A
10861086
1 2.0 PASS 3.00
10871087
10881088
Using a callable ``formatter`` function.
10891089
10901090
>>> func = lambda s: 'STRING' if isinstance(s, str) else 'FLOAT'
1091-
>>> df.style.format({0: '{:.1f}', 2: func}, precision=4, na_rep='MISS')
1092-
... # doctest: +SKIP
1091+
>>> df.style.format({0: '{:.1f}', 2: func},
1092+
... precision=4, na_rep='MISS') # doctest: +SKIP
10931093
0 1 2
10941094
0 MISS 1.0000 STRING
10951095
1 2.0 MISS FLOAT
@@ -1098,8 +1098,7 @@ def format(
10981098
10991099
>>> df = pd.DataFrame([['<div></div>', '"A&B"', None]])
11001100
>>> s = df.style.format(
1101-
... '<a href="a.com/{0}">{0}</a>', escape="html", na_rep="NA"
1102-
... )
1101+
... '<a href="a.com/{0}">{0}</a>', escape="html", na_rep="NA")
11031102
>>> s.to_html() # doctest: +SKIP
11041103
...
11051104
<td .. ><a href="a.com/&lt;div&gt;&lt;/div&gt;">&lt;div&gt;&lt;/div&gt;</a></td>
@@ -1110,8 +1109,8 @@ def format(
11101109
Using a ``formatter`` with ``escape`` in 'latex' mode.
11111110
11121111
>>> df = pd.DataFrame([["123"], ["~ ^"], ["$%#"]])
1113-
>>> df.style.format("\\textbf{{{}}}", escape="latex").to_latex()
1114-
... # doctest: +SKIP
1112+
>>> df.style.format("\\textbf{{{}}}",
1113+
... escape="latex").to_latex() # doctest: +SKIP
11151114
\begin{tabular}{ll}
11161115
& 0 \\
11171116
0 & \textbf{123} \\
@@ -1122,10 +1121,10 @@ def format(
11221121
Applying ``escape`` in 'latex-math' mode. In the example below
11231122
we enter math mode using the character ``$``.
11241123
1125-
>>> df = pd.DataFrame([[r"$\sum_{i=1}^{10} a_i$ a~b $\alpha \
1126-
... = \frac{\beta}{\zeta^2}$"], ["%#^ $ \$x^2 $"]])
1127-
>>> df.style.format(escape="latex-math").to_latex()
1128-
... # doctest: +SKIP
1124+
>>> df = pd.DataFrame([
1125+
... [r"$\sum_{i=1}^{10} a_i$ a~b $\alpha = \frac{\beta}{\zeta^2}$"],
1126+
... [r"%#^ $ \$x^2 $"]])
1127+
>>> df.style.format(escape="latex-math").to_latex() # doctest: +SKIP
11291128
\begin{tabular}{ll}
11301129
& 0 \\
11311130
0 & $\sum_{i=1}^{10} a_i$ a\textasciitilde b $\alpha = \frac{\beta}{\zeta^2}$ \\
@@ -1135,10 +1134,10 @@ def format(
11351134
We can use the character ``\(`` to enter math mode and the character ``\)``
11361135
to close math mode.
11371136
1138-
>>> df = pd.DataFrame([[r"\(\sum_{i=1}^{10} a_i\) a~b \(\alpha \
1139-
... = \frac{\beta}{\zeta^2}\)"], ["%#^ \( \$x^2 \)"]])
1140-
>>> df.style.format(escape="latex-math").to_latex()
1141-
... # doctest: +SKIP
1137+
>>> df = pd.DataFrame([
1138+
... [r"\(\sum_{i=1}^{10} a_i\) a~b \(\alpha = \frac{\beta}{\zeta^2}\)"],
1139+
... [r"%#^ \( \$x^2 \)"]])
1140+
>>> df.style.format(escape="latex-math").to_latex() # doctest: +SKIP
11421141
\begin{tabular}{ll}
11431142
& 0 \\
11441143
0 & \(\sum_{i=1}^{10} a_i\) a\textasciitilde b \(\alpha
@@ -1149,10 +1148,10 @@ def format(
11491148
If we have in one DataFrame cell a combination of both shorthands
11501149
for math formulas, the shorthand with the sign ``$`` will be applied.
11511150
1152-
>>> df = pd.DataFrame([[r"\( x^2 \) $x^2$"], \
1151+
>>> df = pd.DataFrame([
1152+
... [r"\( x^2 \) $x^2$"],
11531153
... [r"$\frac{\beta}{\zeta}$ \(\frac{\beta}{\zeta}\)"]])
1154-
>>> df.style.format(escape="latex-math").to_latex()
1155-
... # doctest: +SKIP
1154+
>>> df.style.format(escape="latex-math").to_latex() # doctest: +SKIP
11561155
\begin{tabular}{ll}
11571156
& 0 \\
11581157
0 & \textbackslash ( x\textasciicircum 2 \textbackslash ) $x^2$ \\
@@ -1169,7 +1168,7 @@ def format(
11691168
>>> df = pd.DataFrame({"A": [1, 0, -1]})
11701169
>>> pseudo_css = "number-format: 0§[Red](0)§-§@;"
11711170
>>> filename = "formatted_file.xlsx"
1172-
>>> df.style.map(lambda v: pseudo_css).to_excel(filename) # doctest: +SKIP
1171+
>>> df.style.map(lambda v: pseudo_css).to_excel(filename) # doctest: +SKIP
11731172
11741173
.. figure:: ../../_static/style/format_excel_css.png
11751174
"""

0 commit comments

Comments
 (0)