Skip to content

Commit 2b2ca71

Browse files
ma3dameeseeksmachine
authored andcommitted
Backport PR pandas-dev#38247: BUG: Limited available colors
1 parent 5b1585a commit 2b2ca71

File tree

4 files changed

+176
-22
lines changed

4 files changed

+176
-22
lines changed

doc/source/whatsnew/v1.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ Other enhancements
308308
- Improved error reporting for subsetting columns of a :class:`.DataFrameGroupBy` with ``axis=1`` (:issue:`37725`)
309309
- Implement method ``cross`` for :meth:`DataFrame.merge` and :meth:`DataFrame.join` (:issue:`5401`)
310310
- When :func:`read_csv/sas/json` are called with ``chuncksize``/``iterator`` they can be used in a ``with`` statement as they return context-managers (:issue:`38225`)
311+
- Augmented the list of named colors available for styling Excel exports, enabling all of CSS4 colors (:issue:`38247`)
311312

312313
.. ---------------------------------------------------------------------------
313314

pandas/io/formats/_color_data.py

+155
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
# GH37967: Enable the use of CSS named colors, as defined in
2+
# matplotlib.colors.CSS4_COLORS, when exporting to Excel.
3+
# This data has been copied here, instead of being imported from matplotlib,
4+
# not to have ``to_excel`` methods require matplotlib.
5+
# source: matplotlib._color_data (3.3.3)
6+
CSS4_COLORS = {
7+
"aliceblue": "F0F8FF",
8+
"antiquewhite": "FAEBD7",
9+
"aqua": "00FFFF",
10+
"aquamarine": "7FFFD4",
11+
"azure": "F0FFFF",
12+
"beige": "F5F5DC",
13+
"bisque": "FFE4C4",
14+
"black": "000000",
15+
"blanchedalmond": "FFEBCD",
16+
"blue": "0000FF",
17+
"blueviolet": "8A2BE2",
18+
"brown": "A52A2A",
19+
"burlywood": "DEB887",
20+
"cadetblue": "5F9EA0",
21+
"chartreuse": "7FFF00",
22+
"chocolate": "D2691E",
23+
"coral": "FF7F50",
24+
"cornflowerblue": "6495ED",
25+
"cornsilk": "FFF8DC",
26+
"crimson": "DC143C",
27+
"cyan": "00FFFF",
28+
"darkblue": "00008B",
29+
"darkcyan": "008B8B",
30+
"darkgoldenrod": "B8860B",
31+
"darkgray": "A9A9A9",
32+
"darkgreen": "006400",
33+
"darkgrey": "A9A9A9",
34+
"darkkhaki": "BDB76B",
35+
"darkmagenta": "8B008B",
36+
"darkolivegreen": "556B2F",
37+
"darkorange": "FF8C00",
38+
"darkorchid": "9932CC",
39+
"darkred": "8B0000",
40+
"darksalmon": "E9967A",
41+
"darkseagreen": "8FBC8F",
42+
"darkslateblue": "483D8B",
43+
"darkslategray": "2F4F4F",
44+
"darkslategrey": "2F4F4F",
45+
"darkturquoise": "00CED1",
46+
"darkviolet": "9400D3",
47+
"deeppink": "FF1493",
48+
"deepskyblue": "00BFFF",
49+
"dimgray": "696969",
50+
"dimgrey": "696969",
51+
"dodgerblue": "1E90FF",
52+
"firebrick": "B22222",
53+
"floralwhite": "FFFAF0",
54+
"forestgreen": "228B22",
55+
"fuchsia": "FF00FF",
56+
"gainsboro": "DCDCDC",
57+
"ghostwhite": "F8F8FF",
58+
"gold": "FFD700",
59+
"goldenrod": "DAA520",
60+
"gray": "808080",
61+
"green": "008000",
62+
"greenyellow": "ADFF2F",
63+
"grey": "808080",
64+
"honeydew": "F0FFF0",
65+
"hotpink": "FF69B4",
66+
"indianred": "CD5C5C",
67+
"indigo": "4B0082",
68+
"ivory": "FFFFF0",
69+
"khaki": "F0E68C",
70+
"lavender": "E6E6FA",
71+
"lavenderblush": "FFF0F5",
72+
"lawngreen": "7CFC00",
73+
"lemonchiffon": "FFFACD",
74+
"lightblue": "ADD8E6",
75+
"lightcoral": "F08080",
76+
"lightcyan": "E0FFFF",
77+
"lightgoldenrodyellow": "FAFAD2",
78+
"lightgray": "D3D3D3",
79+
"lightgreen": "90EE90",
80+
"lightgrey": "D3D3D3",
81+
"lightpink": "FFB6C1",
82+
"lightsalmon": "FFA07A",
83+
"lightseagreen": "20B2AA",
84+
"lightskyblue": "87CEFA",
85+
"lightslategray": "778899",
86+
"lightslategrey": "778899",
87+
"lightsteelblue": "B0C4DE",
88+
"lightyellow": "FFFFE0",
89+
"lime": "00FF00",
90+
"limegreen": "32CD32",
91+
"linen": "FAF0E6",
92+
"magenta": "FF00FF",
93+
"maroon": "800000",
94+
"mediumaquamarine": "66CDAA",
95+
"mediumblue": "0000CD",
96+
"mediumorchid": "BA55D3",
97+
"mediumpurple": "9370DB",
98+
"mediumseagreen": "3CB371",
99+
"mediumslateblue": "7B68EE",
100+
"mediumspringgreen": "00FA9A",
101+
"mediumturquoise": "48D1CC",
102+
"mediumvioletred": "C71585",
103+
"midnightblue": "191970",
104+
"mintcream": "F5FFFA",
105+
"mistyrose": "FFE4E1",
106+
"moccasin": "FFE4B5",
107+
"navajowhite": "FFDEAD",
108+
"navy": "000080",
109+
"oldlace": "FDF5E6",
110+
"olive": "808000",
111+
"olivedrab": "6B8E23",
112+
"orange": "FFA500",
113+
"orangered": "FF4500",
114+
"orchid": "DA70D6",
115+
"palegoldenrod": "EEE8AA",
116+
"palegreen": "98FB98",
117+
"paleturquoise": "AFEEEE",
118+
"palevioletred": "DB7093",
119+
"papayawhip": "FFEFD5",
120+
"peachpuff": "FFDAB9",
121+
"peru": "CD853F",
122+
"pink": "FFC0CB",
123+
"plum": "DDA0DD",
124+
"powderblue": "B0E0E6",
125+
"purple": "800080",
126+
"rebeccapurple": "663399",
127+
"red": "FF0000",
128+
"rosybrown": "BC8F8F",
129+
"royalblue": "4169E1",
130+
"saddlebrown": "8B4513",
131+
"salmon": "FA8072",
132+
"sandybrown": "F4A460",
133+
"seagreen": "2E8B57",
134+
"seashell": "FFF5EE",
135+
"sienna": "A0522D",
136+
"silver": "C0C0C0",
137+
"skyblue": "87CEEB",
138+
"slateblue": "6A5ACD",
139+
"slategray": "708090",
140+
"slategrey": "708090",
141+
"snow": "FFFAFA",
142+
"springgreen": "00FF7F",
143+
"steelblue": "4682B4",
144+
"tan": "D2B48C",
145+
"teal": "008080",
146+
"thistle": "D8BFD8",
147+
"tomato": "FF6347",
148+
"turquoise": "40E0D0",
149+
"violet": "EE82EE",
150+
"wheat": "F5DEB3",
151+
"white": "FFFFFF",
152+
"whitesmoke": "F5F5F5",
153+
"yellow": "FFFF00",
154+
"yellowgreen": "9ACD32",
155+
}

pandas/io/formats/excel.py

+2-22
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from pandas.core import generic
2222
import pandas.core.common as com
2323

24+
from pandas.io.formats._color_data import CSS4_COLORS
2425
from pandas.io.formats.css import CSSResolver, CSSWarning
2526
from pandas.io.formats.format import get_level_lengths
2627
from pandas.io.formats.printing import pprint_thing
@@ -65,28 +66,7 @@ class CSSToExcelConverter:
6566
CSS processed by :meth:`__call__`.
6667
"""
6768

68-
NAMED_COLORS = {
69-
"maroon": "800000",
70-
"brown": "A52A2A",
71-
"red": "FF0000",
72-
"pink": "FFC0CB",
73-
"orange": "FFA500",
74-
"yellow": "FFFF00",
75-
"olive": "808000",
76-
"green": "008000",
77-
"purple": "800080",
78-
"fuchsia": "FF00FF",
79-
"lime": "00FF00",
80-
"teal": "008080",
81-
"aqua": "00FFFF",
82-
"blue": "0000FF",
83-
"navy": "000080",
84-
"black": "000000",
85-
"gray": "808080",
86-
"grey": "808080",
87-
"silver": "C0C0C0",
88-
"white": "FFFFFF",
89-
}
69+
NAMED_COLORS = CSS4_COLORS
9070

9171
VERTICAL_MAP = {
9272
"top": "top",

pandas/tests/io/formats/test_to_excel.py

+18
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
33
ExcelFormatter is tested implicitly in pandas/tests/io/excel
44
"""
5+
import string
56

67
import pytest
78

9+
import pandas.util._test_decorators as td
10+
811
import pandas._testing as tm
912

1013
from pandas.io.formats.css import CSSWarning
@@ -313,3 +316,18 @@ def test_css_to_excel_bad_colors(input_color):
313316
with tm.assert_produces_warning(CSSWarning):
314317
convert = CSSToExcelConverter()
315318
assert expected == convert(css)
319+
320+
321+
def tests_css_named_colors_valid():
322+
upper_hexs = set(map(str.upper, string.hexdigits))
323+
for color in CSSToExcelConverter.NAMED_COLORS.values():
324+
assert len(color) == 6 and all(c in upper_hexs for c in color)
325+
326+
327+
@td.skip_if_no_mpl
328+
def test_css_named_colors_from_mpl_present():
329+
from matplotlib.colors import CSS4_COLORS as mpl_colors
330+
331+
pd_colors = CSSToExcelConverter.NAMED_COLORS
332+
for name, color in mpl_colors.items():
333+
assert name in pd_colors and pd_colors[name] == color[1:]

0 commit comments

Comments
 (0)