From 32476a264fde933121a7d62b28cff69b140fdd22 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Fri, 7 Jan 2022 20:21:25 +0100 Subject: [PATCH 1/3] use shared doc string --- pandas/io/formats/style.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 29c1e35dbb546..e6eb3d4f0eacb 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -98,6 +98,11 @@ def _mpl(func: Callable): is not used. """ +color = """ + color : str, default 'yellow' + Background color to use for highlighting. +""" + # ### @@ -2949,7 +2954,7 @@ def f(data: DataFrame, props: str) -> np.ndarray: props = f"background-color: {null_color};" return self.apply(f, axis=None, subset=subset, props=props) - @Substitution(subset=subset, props=props) + @Substitution(subset=subset, color=color, props=props) def highlight_max( self, subset: Subset | None = None, @@ -2963,8 +2968,7 @@ def highlight_max( Parameters ---------- %(subset)s - color : str, default 'yellow' - Background color to use for highlighting. + %(color)s axis : {0 or 'index', 1 or 'columns', None}, default 0 Apply to each column (``axis=0`` or ``'index'``), to each row (``axis=1`` or ``'columns'``), or to the entire DataFrame at once @@ -2993,7 +2997,7 @@ def highlight_max( props=props, ) - @Substitution(subset=subset, props=props) + @Substitution(subset=subset, color=color, props=props) def highlight_min( self, subset: Subset | None = None, @@ -3007,8 +3011,7 @@ def highlight_min( Parameters ---------- %(subset)s - color : str, default 'yellow' - Background color to use for highlighting. + %(color)s axis : {0 or 'index', 1 or 'columns', None}, default 0 Apply to each column (``axis=0`` or ``'index'``), to each row (``axis=1`` or ``'columns'``), or to the entire DataFrame at once @@ -3037,7 +3040,7 @@ def highlight_min( props=props, ) - @Substitution(subset=subset, props=props) + @Substitution(subset=subset, color=color, props=props) def highlight_between( self, subset: Subset | None = None, @@ -3056,8 +3059,7 @@ def highlight_between( Parameters ---------- %(subset)s - color : str, default 'yellow' - Background color to use for highlighting. + %(color)s axis : {0 or 'index', 1 or 'columns', None}, default 0 If ``left`` or ``right`` given as sequence, axis along which to apply those boundaries. See examples. @@ -3141,7 +3143,7 @@ def highlight_between( inclusive=inclusive, ) - @Substitution(subset=subset, props=props) + @Substitution(subset=subset, color=color, props=props) def highlight_quantile( self, subset: Subset | None = None, @@ -3161,8 +3163,7 @@ def highlight_quantile( Parameters ---------- %(subset)s - color : str, default 'yellow' - Background color to use for highlighting. + %(color)s axis : {0 or 'index', 1 or 'columns', None}, default 0 Axis along which to determine and highlight quantiles. If ``None`` quantiles are measured over the entire DataFrame. See examples. From 441160edc0895cfab94a1f77ae5eb90fbc4eb9c1 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Fri, 7 Jan 2022 23:51:01 +0100 Subject: [PATCH 2/3] reformat the strings --- pandas/io/formats/excel.py | 6 ++-- pandas/io/formats/style.py | 27 +++++++++-------- pandas/tests/io/excel/test_style.py | 47 ++++++++++++++++++----------- 3 files changed, 46 insertions(+), 34 deletions(-) diff --git a/pandas/io/formats/excel.py b/pandas/io/formats/excel.py index 1f1ca434a22c0..7b76142c343c9 100644 --- a/pandas/io/formats/excel.py +++ b/pandas/io/formats/excel.py @@ -257,13 +257,13 @@ def _border_style(self, style: str | None, width: str | None): # 'thick' # 'thin' if width is None and style is None: - return None + return "none" if style == "none" or style == "hidden": - return None + return "none" width_name = self._get_width_name(width) if width_name is None: - return None + return "none" if style in (None, "groove", "ridge", "inset", "outset", "solid"): # not handled diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index e6eb3d4f0eacb..210979ae538c5 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -85,23 +85,17 @@ def _mpl(func: Callable): #### # Shared Doc Strings -subset = """ - subset : label, array-like, IndexSlice, optional +subset = """subset : label, array-like, IndexSlice, optional A valid 2d input to `DataFrame.loc[]`, or, in the case of a 1d input or single key, to `DataFrame.loc[:, ]` where the columns are - prioritised, to limit ``data`` to *before* applying the function. -""" + prioritised, to limit ``data`` to *before* applying the function.""" -props = """ - props : str, default None - CSS properties to use for highlighting. If ``props`` is given, ``color`` - is not used. -""" +props = """props : str, default None + CSS properties to use for highlighting. If ``props`` is given, ``color`` + is not used.""" -color = """ - color : str, default 'yellow' - Background color to use for highlighting. -""" +color = """color : str, default 'yellow' + Background color to use for highlighting.""" # ### @@ -2931,8 +2925,11 @@ def highlight_null( ---------- null_color : str, default 'red' %(subset)s + .. versionadded:: 1.1.0 + %(props)s + .. versionadded:: 1.3.0 Returns @@ -2974,6 +2971,7 @@ def highlight_max( (``axis=1`` or ``'columns'``), or to the entire DataFrame at once with ``axis=None``. %(props)s + .. versionadded:: 1.3.0 Returns @@ -3017,6 +3015,7 @@ def highlight_min( (``axis=1`` or ``'columns'``), or to the entire DataFrame at once with ``axis=None``. %(props)s + .. versionadded:: 1.3.0 Returns @@ -3070,6 +3069,7 @@ def highlight_between( inclusive : {'both', 'neither', 'left', 'right'} Identify whether bounds are closed or open. %(props)s + Returns ------- self : Styler @@ -3177,6 +3177,7 @@ def highlight_quantile( inclusive : {'both', 'neither', 'left', 'right'} Identify whether quantile bounds are closed or open. %(props)s + Returns ------- self : Styler diff --git a/pandas/tests/io/excel/test_style.py b/pandas/tests/io/excel/test_style.py index 8a142aebd719d..7410c29e2f4e0 100644 --- a/pandas/tests/io/excel/test_style.py +++ b/pandas/tests/io/excel/test_style.py @@ -47,26 +47,37 @@ def test_styler_to_excel_unstyled(engine): shared_style_params = [ + # ( + # "background-color: #111222", + # ["fill", "fgColor", "rgb"], + # {"xlsxwriter": "FF111222", "openpyxl": "00111222"}, + # ), + # ( + # "color: #111222", + # ["font", "color", "value"], + # {"xlsxwriter": "FF111222", "openpyxl": "00111222"}, + # ), + # ("font-family: Arial;", ["font", "name"], "arial"), + # ("font-weight: bold;", ["font", "b"], True), + # ("font-style: italic;", ["font", "i"], True), + # ("text-decoration: underline;", ["font", "u"], "single"), + # ("number-format: $??,???.00;", ["number_format"], "$??,???.00"), + # ("text-align: left;", ["alignment", "horizontal"], "left"), + # ( + # "vertical-align: bottom;", + # ["alignment", "vertical"], + # {"xlsxwriter": None, "openpyxl": "bottom"}, # xswrtr cannot be read by opyxl + # ), + # ("border-bottom-style: solid;", ["border", "bottom", "style"], {"medium"}), ( - "background-color: #111222", - ["fill", "fgColor", "rgb"], - {"xlsxwriter": "FF111222", "openpyxl": "00111222"}, + "border-bottom-color: red;", + ["border", "bottom", "color", "value"], + {"xlsxwriter": None, "openpyxl": "00FF0000"}, # xswrtr cannot be read by opyxl ), ( - "color: #111222", - ["font", "color", "value"], - {"xlsxwriter": "FF111222", "openpyxl": "00111222"}, - ), - ("font-family: Arial;", ["font", "name"], "arial"), - ("font-weight: bold;", ["font", "b"], True), - ("font-style: italic;", ["font", "i"], True), - ("text-decoration: underline;", ["font", "u"], "single"), - ("number-format: $??,???.00;", ["number_format"], "$??,???.00"), - ("text-align: left;", ["alignment", "horizontal"], "left"), - ( - "vertical-align: bottom;", - ["alignment", "vertical"], - {"xlsxwriter": None, "openpyxl": "bottom"}, # xlsxwriter Fails + "border-bottom-width: 2px;", + ["border"], + {"xlsxwriter": None, "openpyxl": "bottom"}, ), ] @@ -93,7 +104,7 @@ def test_styler_to_excel_basic(engine, css, attrs, expected): # test styled cell has expected styles u_cell, s_cell = wb["dataframe"].cell(2, 2), wb["styled"].cell(2, 2) for attr in attrs: - u_cell, s_cell = getattr(u_cell, attr), getattr(s_cell, attr) + u_cell, s_cell = getattr(u_cell, attr, None), getattr(s_cell, attr, None) if isinstance(expected, dict): assert u_cell is None or u_cell != expected[engine] From 90f86b4516c5d0948248e19f627a376857f1b8b4 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Mon, 10 Jan 2022 19:05:59 +0100 Subject: [PATCH 3/3] revert alt branch changes --- pandas/io/formats/excel.py | 6 ++-- pandas/tests/io/excel/test_style.py | 47 +++++++++++------------------ 2 files changed, 21 insertions(+), 32 deletions(-) diff --git a/pandas/io/formats/excel.py b/pandas/io/formats/excel.py index 7b76142c343c9..1f1ca434a22c0 100644 --- a/pandas/io/formats/excel.py +++ b/pandas/io/formats/excel.py @@ -257,13 +257,13 @@ def _border_style(self, style: str | None, width: str | None): # 'thick' # 'thin' if width is None and style is None: - return "none" + return None if style == "none" or style == "hidden": - return "none" + return None width_name = self._get_width_name(width) if width_name is None: - return "none" + return None if style in (None, "groove", "ridge", "inset", "outset", "solid"): # not handled diff --git a/pandas/tests/io/excel/test_style.py b/pandas/tests/io/excel/test_style.py index 7410c29e2f4e0..8a142aebd719d 100644 --- a/pandas/tests/io/excel/test_style.py +++ b/pandas/tests/io/excel/test_style.py @@ -47,37 +47,26 @@ def test_styler_to_excel_unstyled(engine): shared_style_params = [ - # ( - # "background-color: #111222", - # ["fill", "fgColor", "rgb"], - # {"xlsxwriter": "FF111222", "openpyxl": "00111222"}, - # ), - # ( - # "color: #111222", - # ["font", "color", "value"], - # {"xlsxwriter": "FF111222", "openpyxl": "00111222"}, - # ), - # ("font-family: Arial;", ["font", "name"], "arial"), - # ("font-weight: bold;", ["font", "b"], True), - # ("font-style: italic;", ["font", "i"], True), - # ("text-decoration: underline;", ["font", "u"], "single"), - # ("number-format: $??,???.00;", ["number_format"], "$??,???.00"), - # ("text-align: left;", ["alignment", "horizontal"], "left"), - # ( - # "vertical-align: bottom;", - # ["alignment", "vertical"], - # {"xlsxwriter": None, "openpyxl": "bottom"}, # xswrtr cannot be read by opyxl - # ), - # ("border-bottom-style: solid;", ["border", "bottom", "style"], {"medium"}), ( - "border-bottom-color: red;", - ["border", "bottom", "color", "value"], - {"xlsxwriter": None, "openpyxl": "00FF0000"}, # xswrtr cannot be read by opyxl + "background-color: #111222", + ["fill", "fgColor", "rgb"], + {"xlsxwriter": "FF111222", "openpyxl": "00111222"}, ), ( - "border-bottom-width: 2px;", - ["border"], - {"xlsxwriter": None, "openpyxl": "bottom"}, + "color: #111222", + ["font", "color", "value"], + {"xlsxwriter": "FF111222", "openpyxl": "00111222"}, + ), + ("font-family: Arial;", ["font", "name"], "arial"), + ("font-weight: bold;", ["font", "b"], True), + ("font-style: italic;", ["font", "i"], True), + ("text-decoration: underline;", ["font", "u"], "single"), + ("number-format: $??,???.00;", ["number_format"], "$??,???.00"), + ("text-align: left;", ["alignment", "horizontal"], "left"), + ( + "vertical-align: bottom;", + ["alignment", "vertical"], + {"xlsxwriter": None, "openpyxl": "bottom"}, # xlsxwriter Fails ), ] @@ -104,7 +93,7 @@ def test_styler_to_excel_basic(engine, css, attrs, expected): # test styled cell has expected styles u_cell, s_cell = wb["dataframe"].cell(2, 2), wb["styled"].cell(2, 2) for attr in attrs: - u_cell, s_cell = getattr(u_cell, attr, None), getattr(s_cell, attr, None) + u_cell, s_cell = getattr(u_cell, attr), getattr(s_cell, attr) if isinstance(expected, dict): assert u_cell is None or u_cell != expected[engine]