Skip to content

Commit 232cd59

Browse files
committed
Fix mapping discrete column to integer values
fixes has2k1#108
1 parent 9b068b4 commit 232cd59

3 files changed

Lines changed: 26 additions & 1 deletion

File tree

doc/changelog.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ Bug Fixes
4848
ignored when determining the axis titles. Now, the ``name`` parameter
4949
is specified, it is used as the title. (:issue:`105`)
5050

51+
- Fixed bug in discrete scales where a column could not be mapped
52+
to integer values. (:issue:`108`)
53+
5154
v0.3.0
5255
------
5356
*(2017-11-08)*

plotnine/scales/scale.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,9 @@ def map(self, x, limits=None):
284284
else:
285285
pal = np.asarray(pal)
286286
pal_match = pal[match(x, limits)]
287-
pal_match[pd.isnull(pal_match)] = self.na_value
287+
bool_idx = pd.isnull(pal_match)
288+
if np.any(bool_idx):
289+
pal_match[bool_idx] = self.na_value
288290
return pal_match
289291

290292
def break_info(self, range=None):

plotnine/tests/test_scale_internals.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from datetime import datetime
44

55
import numpy as np
6+
import numpy.testing as npt
67
import pandas as pd
78
import pytest
89
import six
@@ -22,6 +23,7 @@
2223
scale_size_continuous,
2324
scale_size_area,
2425
scale_size_radius)
26+
from plotnine.scales.scale_manual import _scale_manual
2527
from plotnine.scales.scales import make_scale
2628
from plotnine.exceptions import PlotnineError
2729

@@ -378,3 +380,21 @@ def test_scale_without_a_mapping():
378380
+ scale_color.scale_color_continuous())
379381
with pytest.warns(UserWarning):
380382
p.draw_test()
383+
384+
385+
def scale_scale_discrete_mapping_nulls():
386+
a = np.array([1, 2, 3], dtype=object)
387+
388+
sc = _scale_manual([1, 2, 3, 4, 5])
389+
sc.train(a)
390+
res = sc.map([1, 2, 3])
391+
expected = np.array([1, 2, 3])
392+
npt.assert_array_equal(res, expected)
393+
394+
sc = _scale_manual([1, None, 3, 4, 5])
395+
sc.train(a)
396+
res = sc.map([1, 2, 3])
397+
expected = np.array([1, np.nan, 3])
398+
assert res[0] == expected[0]
399+
assert res[1] is expected[1]
400+
assert res[2] == expected[2]

0 commit comments

Comments
 (0)