Skip to content

Commit 92f36ce

Browse files
committed
Work around a bug in pandas.DataFrame.replace to set the color
In the case a original value (e.g. 0-9A-F) was part of a color defintion, using df.replace{dict} would do a double replace on the same cell, resulting in colors like "#12345#ABCDEF", which would then throw an error during plotting. Using apply(lambda x: replacements[x]) is probably slower, so should be replaced when the pandas bug is gone. See pandas-dev/pandas#5338 for the bugreport in pandas.
1 parent 907d6df commit 92f36ce

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

ggplot/ggplot.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,15 @@ def _get_layers(self, data=None):
331331
else:
332332
color_mapping = {value: color.next() for value in possible_colors}
333333
rev_color_mapping = {v: k for k, v in color_mapping.items()}
334-
mapping.color = mapping.color.replace(color_mapping)
334+
# replace does not work in some cases: https://github.com/pydata/pandas/issues/5338
335+
# "6" -> "#123456" would end up like "#12345#123456"
336+
# Use a workaround which is hopefully not too bad when we only have a few unique values
337+
#mapping.color = mapping.color.replace(color_mapping)
338+
mapping.color = mapping.color.apply(lambda x: color_mapping[x])
335339

336340
rev_shape_mapping = {}
337341
if 'shape' in mapping:
342+
#Todo: also look if the shapes are already useable like in the color case?
338343
possible_shapes = np.unique(mapping['shape'])
339344
shape = shapes.shape_gen()
340345
shape_mapping = {value: shape.next() for value in possible_shapes}
@@ -344,6 +349,7 @@ def _get_layers(self, data=None):
344349

345350
rev_linetype_mapping = {}
346351
if 'linestyle' in mapping:
352+
#Todo: also look if the linestyles are already useable like in the color case?
347353
mapping['linestyle'] = mapping['linestyle'].apply(str)
348354
possible_styles = np.unique(mapping['linestyle'])
349355
linestyle = linestyles.line_gen()

0 commit comments

Comments
 (0)