From 726330701134cf405214bb77585d2af2f7a7a2ac Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Thu, 18 Feb 2021 17:40:38 +0100 Subject: [PATCH 1/4] BUG: relative luminance calc --- 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 1e2148125a9d1..1e86281c04f5c 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -1441,7 +1441,7 @@ def relative_luminance(rgba) -> float: The relative luminance as a value from 0 to 1 """ r, g, b = ( - x / 12.92 if x <= 0.03928 else ((x + 0.055) / 1.055 ** 2.4) + x / 12.92 if x <= 0.04045 else ((x + 0.055) / 1.055) ** 2.4 for x in rgba[:3] ) return 0.2126 * r + 0.7152 * g + 0.0722 * b From c09ddaf4d04435446d996a9cdbe0f2cc63e83cdf Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Thu, 18 Feb 2021 17:48:53 +0100 Subject: [PATCH 2/4] BUG: relative luminance calc --- doc/source/whatsnew/v1.3.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index bf67ff6525005..d18dfa0f071ca 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -475,6 +475,7 @@ Other - Bug in :class:`Styler` where ``subset`` arg in methods raised an error for some valid multiindex slices (:issue:`33562`) - :class:`Styler` rendered HTML output minor alterations to support w3 good code standard (:issue:`39626`) - Bug in :class:`Styler` where rendered HTML was missing a column class identifier for certain header cells (:issue:`39716`) +- Bug in :meth:`Styler.background_gradient` where text-color was not determined correctly (:issue:`39888`) - Bug in :meth:`DataFrame.equals`, :meth:`Series.equals`, :meth:`Index.equals` with object-dtype containing ``np.datetime64("NaT")`` or ``np.timedelta64("NaT")`` (:issue:`39650`) From 3ecc570d841e802ac4a3006afb720353d4af408b Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Thu, 18 Feb 2021 18:56:40 +0100 Subject: [PATCH 3/4] tests added --- pandas/tests/io/formats/test_style.py | 28 +++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/pandas/tests/io/formats/test_style.py b/pandas/tests/io/formats/test_style.py index b8df18d593667..45dadfb16ad35 100644 --- a/pandas/tests/io/formats/test_style.py +++ b/pandas/tests/io/formats/test_style.py @@ -1994,28 +1994,36 @@ def test_background_gradient(self): assert result[(1, 0)] == ["background-color: #fff7fb", "color: #000000"] @pytest.mark.parametrize( - "c_map,expected", + "cmap, expected", [ ( - None, + "PuBu", { - (0, 0): ["background-color: #440154", "color: #f1f1f1"], - (1, 0): ["background-color: #fde725", "color: #000000"], + (4, 5): ["background-color: #86b0d3", "color: #000000"], + (4, 6): ["background-color: #83afd3", "color: #f1f1f1"], }, ), ( "YlOrRd", { - (0, 0): ["background-color: #ffffcc", "color: #000000"], - (1, 0): ["background-color: #800026", "color: #f1f1f1"], + (4, 8): ["background-color: #fd913e", "color: #000000"], + (4, 9): ["background-color: #fd8f3d", "color: #f1f1f1"], + }, + ), + ( + None, + { + (7, 0): ["background-color: #48c16e", "color: #f1f1f1"], + (7, 1): ["background-color: #4cc26c", "color: #000000"], }, ), ], ) - def test_text_color_threshold(self, c_map, expected): - df = DataFrame([1, 2], columns=["A"]) - result = df.style.background_gradient(cmap=c_map)._compute().ctx - assert result == expected + def test_text_color_threshold(self, cmap, expected): + df = DataFrame(np.arange(100).reshape(10, 10)) + result = df.style.background_gradient(cmap=cmap, axis=None)._compute().ctx + for k in expected.keys(): + assert result[k] == expected[k] @pytest.mark.parametrize("text_color_threshold", [1.1, "1", -1, [2, 2]]) def test_text_color_threshold_raises(self, text_color_threshold): From 9783bb51ba9abb40bc389ddcf2d11428fd1e2fc5 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Fri, 19 Feb 2021 07:17:24 +0100 Subject: [PATCH 4/4] adj tests --- pandas/tests/io/formats/test_style.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pandas/tests/io/formats/test_style.py b/pandas/tests/io/formats/test_style.py index e3d302673f000..edb2592625a3d 100644 --- a/pandas/tests/io/formats/test_style.py +++ b/pandas/tests/io/formats/test_style.py @@ -1948,22 +1948,22 @@ def test_background_gradient(self): ( "PuBu", { - (4, 5): ["background-color: #86b0d3", "color: #000000"], - (4, 6): ["background-color: #83afd3", "color: #f1f1f1"], + (4, 5): [("background-color", "#86b0d3"), ("color", "#000000")], + (4, 6): [("background-color", "#83afd3"), ("color", "#f1f1f1")], }, ), ( "YlOrRd", { - (4, 8): ["background-color: #fd913e", "color: #000000"], - (4, 9): ["background-color: #fd8f3d", "color: #f1f1f1"], + (4, 8): [("background-color", "#fd913e"), ("color", "#000000")], + (4, 9): [("background-color", "#fd8f3d"), ("color", "#f1f1f1")], }, ), ( None, { - (7, 0): ["background-color: #48c16e", "color: #f1f1f1"], - (7, 1): ["background-color: #4cc26c", "color: #000000"], + (7, 0): [("background-color", "#48c16e"), ("color", "#f1f1f1")], + (7, 1): [("background-color", "#4cc26c"), ("color", "#000000")], }, ), ],