4
4
"""
5
5
from __future__ import (absolute_import , division , print_function ,
6
6
unicode_literals )
7
-
8
7
import six
9
8
10
9
import numpy as np
11
10
12
- import matplotlib .cbook as cbook
13
11
import matplotlib .units as units
14
12
import matplotlib .ticker as ticker
15
13
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
+
16
36
17
37
class StrCategoryConverter (units .ConversionInterface ):
18
38
@staticmethod
@@ -25,7 +45,8 @@ def convert(value, unit, axis):
25
45
if isinstance (value , six .string_types ):
26
46
return vmap [value ]
27
47
28
- vals = np .array (value , dtype = np .unicode )
48
+ vals = shim_array (value )
49
+
29
50
for lab , loc in vmap .items ():
30
51
vals [vals == lab ] = loc
31
52
@@ -81,8 +102,7 @@ def update(self, new_data):
81
102
self ._set_seq_locs (new_data , value )
82
103
83
104
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 )
86
106
new_s = [d for d in np .unique (strdata ) if d not in self .seq ]
87
107
for ns in new_s :
88
108
self .seq .append (ns )
0 commit comments