Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 52413e0

Browse files
authored
Merge pull request #8591 from story645/cattests
shims for categorical support for numpy < 1.8
2 parents 48584c7 + cc27905 commit 52413e0

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

lib/matplotlib/category.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,35 @@
44
"""
55
from __future__ import (absolute_import, division, print_function,
66
unicode_literals)
7-
87
import six
98

109
import numpy as np
1110

12-
import matplotlib.cbook as cbook
1311
import matplotlib.units as units
1412
import matplotlib.ticker as ticker
1513

14+
# np 1.6/1.7 support
15+
from distutils.version import LooseVersion
16+
import collections
17+
18+
19+
if LooseVersion(np.__version__) >= LooseVersion('1.8.0'):
20+
def shim_array(data):
21+
return np.array(data, dtype=np.unicode)
22+
else:
23+
def shim_array(data):
24+
if (isinstance(data, six.string_types) or
25+
not isinstance(data, collections.Iterable)):
26+
data = [data]
27+
try:
28+
data = [str(d) for d in data]
29+
except UnicodeEncodeError:
30+
# this yields gibberish but unicode text doesn't
31+
# render under numpy1.6 anyway
32+
data = [d.encode('utf-8', 'ignore').decode('utf-8')
33+
for d in data]
34+
return np.array(data, dtype=np.unicode)
35+
1636

1737
class StrCategoryConverter(units.ConversionInterface):
1838
@staticmethod
@@ -25,7 +45,8 @@ def convert(value, unit, axis):
2545
if isinstance(value, six.string_types):
2646
return vmap[value]
2747

28-
vals = np.array(value, dtype=np.unicode)
48+
vals = shim_array(value)
49+
2950
for lab, loc in vmap.items():
3051
vals[vals == lab] = loc
3152

@@ -81,8 +102,7 @@ def update(self, new_data):
81102
self._set_seq_locs(new_data, value)
82103

83104
def _set_seq_locs(self, data, value):
84-
strdata = np.array(data, dtype=np.unicode)
85-
# np.unique makes dateframes work
105+
strdata = shim_array(data)
86106
new_s = [d for d in np.unique(strdata) if d not in self.seq]
87107
for ns in new_s:
88108
self.seq.append(ns)

lib/matplotlib/tests/test_category.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
from __future__ import (absolute_import, division, print_function,
44
unicode_literals)
55

6-
from distutils.version import LooseVersion
7-
86
import pytest
97
import numpy as np
108

@@ -14,11 +12,6 @@
1412
import unittest
1513

1614

17-
needs_new_numpy = pytest.mark.xfail(
18-
LooseVersion(np.__version__) < LooseVersion('1.8.0'),
19-
reason='NumPy < 1.8.0 is broken.')
20-
21-
2215
class TestUnitData(object):
2316
testdata = [("hello world", ["hello world"], [0]),
2417
("Здравствуйте мир", ["Здравствуйте мир"], [0]),
@@ -28,14 +21,12 @@ class TestUnitData(object):
2821

2922
ids = ["single", "unicode", "mixed"]
3023

31-
@needs_new_numpy
3224
@pytest.mark.parametrize("data, seq, locs", testdata, ids=ids)
3325
def test_unit(self, data, seq, locs):
3426
act = cat.UnitData(data)
3527
assert act.seq == seq
3628
assert act.locs == locs
3729

38-
@needs_new_numpy
3930
def test_update_map(self):
4031
data = ['a', 'd']
4132
oseq = ['a', 'd']
@@ -87,7 +78,6 @@ class TestStrCategoryConverter(object):
8778
def mock_axis(self, request):
8879
self.cc = cat.StrCategoryConverter()
8980

90-
@needs_new_numpy
9181
@pytest.mark.parametrize("data, unitmap, exp", testdata, ids=ids)
9282
def test_convert(self, data, unitmap, exp):
9383
MUD = MockUnitData(unitmap)

0 commit comments

Comments
 (0)