Skip to content

Color text based on background color when using _background_gradient() #21263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 17 additions & 19 deletions pandas/tests/io/formats/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -1036,26 +1036,24 @@ def test_background_gradient(self):
'color: #000000']

@td.skip_if_no_mpl
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these tests need to be in the MatplotlibDep test class? I didn't think this required mpl but could be wrong.

If that is the case then move the @td.skip_if_no_mpl decorator to the class instead of on each function individually.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, they both call background_gradient() which depends on mpl.

def test_text_color_threshold(self):
@pytest.mark.parametrize("c_map", [None, 'YlOrRd'])
def test_text_color_threshold(self, c_map):
df = pd.DataFrame([[1, 2], [2, 4]], columns=['A', 'B'])
for c_map in [None, 'YlOrRd']:
result = df.style.background_gradient(cmap=c_map)._compute().ctx
for res in result:
bg_color = result[res][0].split(' ')[1]
assert bg_color in ['#fde725', '#ffffcc',
'#800026', '#440154'], (
"Unexpected background color returned from "
"`style.background_gradient()`")
text_color = result[(res)][1].split(' ')[1]
if bg_color in ['#fde725', '#ffffcc']:
assert text_color == '#000000'
elif bg_color in ['#800026', '#440154']:
assert text_color == '#f1f1f1'
for res in result:
if result[res][0].split(' ')[1] in ['#fde725', '#ffffcc']:
assert result[(res)][1].split(' ')[1] == '#000000'
elif result[res][0].split(' ')[1] in ['#800026', '#440154']:
assert result[(res)][1].split(' ')[1] == '#f1f1f1'
result = df.style.background_gradient(cmap=c_map)._compute().ctx
test_colors = {None: {(0, 0): ('#440154', '#f1f1f1'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change the variable here to expected (standard in pandas tests)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using a nested dict you should also send this in via parametrization, so you would have "c_map,expected" and then send in a tuple of the c_map and it's expected result

(1, 0): ('#fde725', '#000000')},
'YlOrRd': {(0, 0): ('#ffffcc', '#000000'),
(1, 0): ('#800026', '#f1f1f1')}}
# Light text on dark background
assert result[0, 0][0].split(' ')[1] == test_colors[c_map][0, 0][0], (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you splitting this? Just make your expected variable account for the exact string required. Unless I'm missing something you should just have one assertion here for result == expected

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was doing this to be able to raise different assertion messages for the background and foreground color. But since pytest shows the expected and current value anyways, this should be easy to trace done without separate assertion messages.

'Unexpected background color returned from '
'`style.background_gradient()`')
assert result[0, 0][1].split(' ')[1] == test_colors[c_map][0, 0][1]
# Dark text on light background
assert result[1, 0][0].split(' ')[1] == test_colors[c_map][1, 0][0], (
'Unexpected background color returned from '
'`style.background_gradient()`')
assert result[1, 0][1].split(' ')[1] == test_colors[c_map][1, 0][1]

@td.skip_if_no_mpl
@pytest.mark.parametrize("text_color_threshold", [1.1, '1', -1, [2, 2]])
Expand Down