From 2d6705dd0be7d5893ff623230e84c0a0d850f18b Mon Sep 17 00:00:00 2001 From: Zhengbo Wang <77875500+luke396@users.noreply.github.com> Date: Tue, 16 Jan 2024 15:22:37 +0800 Subject: [PATCH 1/7] Fix exo3 and remove unused code --- ci/code_checks.sh | 4 --- pandas/io/formats/style.py | 21 +++++++-------- pandas/io/formats/style_render.py | 45 +++++++++++++++---------------- 3 files changed, 32 insertions(+), 38 deletions(-) diff --git a/ci/code_checks.sh b/ci/code_checks.sh index a08a0cbd87383..3d23ca854853c 100755 --- a/ci/code_checks.sh +++ b/ci/code_checks.sh @@ -109,10 +109,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then pandas.MultiIndex.droplevel \ pandas.IndexSlice \ pandas.Grouper \ - 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 \ pandas.io.formats.style.Styler.format_index \ pandas.io.formats.style.Styler.relabel_index \ pandas.io.formats.style.Styler.hide \ diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index b62f7581ac220..f8ef2a4eecbac 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -1927,7 +1927,7 @@ def apply_index( -------- Basic usage to conditionally highlight values in the index. - >>> df = pd.DataFrame([[1,2], [3,4]], index=["A", "B"]) + >>> df = pd.DataFrame([[1, 2], [3, 4]], index=["A", "B"]) >>> def color_b(s): ... return {ret} >>> df.style.{this}_index(color_b) # doctest: +SKIP @@ -1940,8 +1940,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 """ @@ -1964,7 +1964,7 @@ def apply_index( 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', + ret='"background-color: yellow;" if s == "B" else None', ret2='"background-color: yellow;" if "x" in v else None', ) def map_index( @@ -2068,17 +2068,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. diff --git a/pandas/io/formats/style_render.py b/pandas/io/formats/style_render.py index 55541e5262719..db894a2d162da 100644 --- a/pandas/io/formats/style_render.py +++ b/pandas/io/formats/style_render.py @@ -1063,15 +1063,15 @@ 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 @@ -1079,8 +1079,8 @@ def format( 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 @@ -1088,8 +1088,8 @@ def format( 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 @@ -1098,8 +1098,7 @@ def format( >>> df = pd.DataFrame([['
', '"A&B"', None]]) >>> s = df.style.format( - ... '{0}', escape="html", na_rep="NA" - ... ) + ... '{0}', escape="html", na_rep="NA") >>> s.to_html() # doctest: +SKIP ... <div></div> @@ -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} \\ @@ -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}$ \\ @@ -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 @@ -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$ \\ @@ -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 """ From ba9777e086574e56875995ae83a75452389b8e8f Mon Sep 17 00:00:00 2001 From: Zhengbo Wang <77875500+luke396@users.noreply.github.com> Date: Tue, 16 Jan 2024 15:29:34 +0800 Subject: [PATCH 2/7] Remove unused in code_checks --- ci/code_checks.sh | 4 ---- 1 file changed, 4 deletions(-) diff --git a/ci/code_checks.sh b/ci/code_checks.sh index 145be3e52f2c0..44cfa39acc755 100755 --- a/ci/code_checks.sh +++ b/ci/code_checks.sh @@ -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 \ pandas.io.formats.style.Styler.set_tooltips \ pandas.io.formats.style.Styler.set_uuid \ pandas.io.formats.style.Styler.pipe \ From 93ce14aabae10916cf7d3e6b713508e6e4c63981 Mon Sep 17 00:00:00 2001 From: Zhengbo Wang <77875500+luke396@users.noreply.github.com> Date: Tue, 16 Jan 2024 16:42:24 +0800 Subject: [PATCH 3/7] Fix 's' to 'v' in Styler.map_index() method --- pandas/io/formats/style.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 9de1a54548a4e..c453b5bf776e9 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -1969,7 +1969,7 @@ def apply_index( 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 s == "B" else None', + ret='"background-color: yellow;" if v == "B" else None', ret2='"background-color: yellow;" if "x" in v else None', ) def map_index( From 3c7a6ed69494258ef09a17947ba76b0ad2d3c2a2 Mon Sep 17 00:00:00 2001 From: Zhengbo Wang <77875500+luke396@users.noreply.github.com> Date: Tue, 16 Jan 2024 17:50:28 +0800 Subject: [PATCH 4/7] Try set 's' to {var} --- pandas/io/formats/style.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index c453b5bf776e9..756ba0dad5e44 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -1933,7 +1933,7 @@ 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): + >>> def color_b({var}): ... return {ret} >>> df.style.{this}_index(color_b) # doctest: +SKIP From a5fcf4337de8ccb8dc5ac8ee0ef1452faa286641 Mon Sep 17 00:00:00 2001 From: Zhengbo Wang <77875500+luke396@users.noreply.github.com> Date: Tue, 16 Jan 2024 22:52:57 +0800 Subject: [PATCH 5/7] Use `label` for more readable variable name --- pandas/io/formats/style.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 756ba0dad5e44..9cd20cfb079ea 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -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, @@ -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, From 673fb396147c29a15de8d003bd54ede0f89fa7f2 Mon Sep 17 00:00:00 2001 From: Zhengbo Wang <77875500+luke396@users.noreply.github.com> Date: Wed, 17 Jan 2024 16:56:56 +0800 Subject: [PATCH 6/7] Fix merge mistake --- ci/code_checks.sh | 4 ---- 1 file changed, 4 deletions(-) diff --git a/ci/code_checks.sh b/ci/code_checks.sh index c0dfbcc03b473..f3bc5260b4a51 100755 --- a/ci/code_checks.sh +++ b/ci/code_checks.sh @@ -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 \ pandas.io.formats.style.Styler.highlight_quantile \ pandas.io.formats.style.Styler.background_gradient \ pandas.io.formats.style.Styler.text_gradient \ From 456f71661a6ec99fdd959a2469c69d9defffb465 Mon Sep 17 00:00:00 2001 From: Zhengbo Wang <77875500+luke396@users.noreply.github.com> Date: Wed, 17 Jan 2024 16:58:47 +0800 Subject: [PATCH 7/7] Fix merge error --- ci/code_checks.sh | 5 ----- 1 file changed, 5 deletions(-) diff --git a/ci/code_checks.sh b/ci/code_checks.sh index f3bc5260b4a51..5e6c02eab574d 100755 --- a/ci/code_checks.sh +++ b/ci/code_checks.sh @@ -80,11 +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.highlight_quantile \ - pandas.io.formats.style.Styler.background_gradient \ - pandas.io.formats.style.Styler.text_gradient \ - pandas.DataFrame.plot.hexbin \ - pandas.DataFrame.plot.line RET=$(($RET + $?)) ; echo $MSG "DONE" fi