Skip to content

Commit c967731

Browse files
authored
BUG: Bg gradient map text color fix (pandas-dev#39889)
1 parent 43968df commit c967731

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ Other
480480
- Bug in :class:`Styler` where ``subset`` arg in methods raised an error for some valid multiindex slices (:issue:`33562`)
481481
- :class:`Styler` rendered HTML output minor alterations to support w3 good code standard (:issue:`39626`)
482482
- Bug in :class:`Styler` where rendered HTML was missing a column class identifier for certain header cells (:issue:`39716`)
483+
- Bug in :meth:`Styler.background_gradient` where text-color was not determined correctly (:issue:`39888`)
483484
- Bug in :meth:`DataFrame.equals`, :meth:`Series.equals`, :meth:`Index.equals` with object-dtype containing ``np.datetime64("NaT")`` or ``np.timedelta64("NaT")`` (:issue:`39650`)
484485

485486

pandas/io/formats/style.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1428,7 +1428,7 @@ def relative_luminance(rgba) -> float:
14281428
The relative luminance as a value from 0 to 1
14291429
"""
14301430
r, g, b = (
1431-
x / 12.92 if x <= 0.03928 else ((x + 0.055) / 1.055 ** 2.4)
1431+
x / 12.92 if x <= 0.04045 else ((x + 0.055) / 1.055) ** 2.4
14321432
for x in rgba[:3]
14331433
)
14341434
return 0.2126 * r + 0.7152 * g + 0.0722 * b

pandas/tests/io/formats/test_style.py

+18-10
Original file line numberDiff line numberDiff line change
@@ -1943,28 +1943,36 @@ def test_background_gradient(self):
19431943
assert result[(1, 0)] == [("background-color", "#fff7fb"), ("color", "#000000")]
19441944

19451945
@pytest.mark.parametrize(
1946-
"c_map,expected",
1946+
"cmap, expected",
19471947
[
19481948
(
1949-
None,
1949+
"PuBu",
19501950
{
1951-
(0, 0): [("background-color", "#440154"), ("color", "#f1f1f1")],
1952-
(1, 0): [("background-color", "#fde725"), ("color", "#000000")],
1951+
(4, 5): [("background-color", "#86b0d3"), ("color", "#000000")],
1952+
(4, 6): [("background-color", "#83afd3"), ("color", "#f1f1f1")],
19531953
},
19541954
),
19551955
(
19561956
"YlOrRd",
19571957
{
1958-
(0, 0): [("background-color", "#ffffcc"), ("color", "#000000")],
1959-
(1, 0): [("background-color", "#800026"), ("color", "#f1f1f1")],
1958+
(4, 8): [("background-color", "#fd913e"), ("color", "#000000")],
1959+
(4, 9): [("background-color", "#fd8f3d"), ("color", "#f1f1f1")],
1960+
},
1961+
),
1962+
(
1963+
None,
1964+
{
1965+
(7, 0): [("background-color", "#48c16e"), ("color", "#f1f1f1")],
1966+
(7, 1): [("background-color", "#4cc26c"), ("color", "#000000")],
19601967
},
19611968
),
19621969
],
19631970
)
1964-
def test_text_color_threshold(self, c_map, expected):
1965-
df = DataFrame([1, 2], columns=["A"])
1966-
result = df.style.background_gradient(cmap=c_map)._compute().ctx
1967-
assert result == expected
1971+
def test_text_color_threshold(self, cmap, expected):
1972+
df = DataFrame(np.arange(100).reshape(10, 10))
1973+
result = df.style.background_gradient(cmap=cmap, axis=None)._compute().ctx
1974+
for k in expected.keys():
1975+
assert result[k] == expected[k]
19681976

19691977
@pytest.mark.parametrize("text_color_threshold", [1.1, "1", -1, [2, 2]])
19701978
def test_text_color_threshold_raises(self, text_color_threshold):

0 commit comments

Comments
 (0)