Skip to content

Inclusion of new NAMED_COLORS for MS Excel #18392

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 2 commits into from
Jan 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions pandas/io/formats/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,9 @@ def build_font(self, props):

NAMED_COLORS = {
'maroon': '800000',
'brown': 'A52A2A',
'red': 'FF0000',
'pink': 'FFC0CB',
'orange': 'FFA500',
'yellow': 'FFFF00',
'olive': '808000',
Expand All @@ -291,6 +293,7 @@ def build_font(self, props):
'navy': '000080',
'black': '000000',
'gray': '808080',
'grey': '808080',
'silver': 'C0C0C0',
'white': 'FFFFFF',
}
Expand Down
60 changes: 60 additions & 0 deletions pandas/tests/io/formats/test_to_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"""

import pytest
import pandas.util.testing as tm

from warnings import catch_warnings
from pandas.io.formats.excel import CSSToExcelConverter


Expand Down Expand Up @@ -212,3 +214,61 @@ def test_css_to_excel_multiple():
def test_css_to_excel_inherited(css, inherited, expected):
convert = CSSToExcelConverter(inherited)
assert expected == convert(css)


@pytest.mark.parametrize("input_color,output_color", (
[(name, rgb) for name, rgb in CSSToExcelConverter.NAMED_COLORS.items()] +
[("#" + rgb, rgb) for rgb in CSSToExcelConverter.NAMED_COLORS.values()] +
[("#F0F", "FF00FF"), ("#ABC", "AABBCC")])
)
def test_css_to_excel_good_colors(input_color, output_color):
# see gh-18392
css = ("border-top-color: {color}; "
"border-right-color: {color}; "
"border-bottom-color: {color}; "
"border-left-color: {color}; "
"background-color: {color}; "
"color: {color}").format(color=input_color)

expected = dict()

expected["fill"] = {
"patternType": "solid",
"fgColor": output_color
}

expected["font"] = {
"color": output_color
}

expected["border"] = {
k: {
"color": output_color,
} for k in ("top", "right", "bottom", "left")
}

with tm.assert_produces_warning(None):
convert = CSSToExcelConverter()
assert expected == convert(css)


@pytest.mark.parametrize("input_color", [None, "not-a-color"])
def test_css_to_excel_bad_colors(input_color):
# see gh-18392
css = ("border-top-color: {color}; "
"border-right-color: {color}; "
"border-bottom-color: {color}; "
"border-left-color: {color}; "
"background-color: {color}; "
"color: {color}").format(color=input_color)

expected = dict()

if input_color is not None:
expected["fill"] = {
"patternType": "solid"
}

with catch_warnings(record=True):
convert = CSSToExcelConverter()
Copy link
Contributor

Choose a reason for hiding this comment

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

so these don't actually raise? rather just show a warning? ok I guess

Copy link
Contributor

Choose a reason for hiding this comment

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

so these don't actually raise? rather just show a warning? ok I guess

Copy link
Contributor

Choose a reason for hiding this comment

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

so we aren't actually testing anything then?

Copy link
Contributor

Choose a reason for hiding this comment

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

note - I don't think they should raise as that puts a burden back on us. is it possible to check the warnings message? (note don't spend too much time on this, if its easy do it).

Copy link
Member

Choose a reason for hiding this comment

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

@jreback : When I call convert with an invalid color in my test case, it issues CSSWarning in 5 - 6 different places because of the invalid color (at least when I introspect with tm.assert_produces_warning).

assert expected == convert(css)