@@ -101,8 +101,6 @@ def convert_to_string(value):
101101class UnitData (object ):
102102 # debatable makes sense to special code missing values
103103 spdict = {'nan' : - 1.0 , 'inf' : - 2.0 , '-inf' : - 3.0 }
104- # used to set out of bounds
105- LOWER_BOUND = - 4
106104
107105 def __init__ (self , data ):
108106 """Create mapping between unique categorical values
@@ -113,32 +111,26 @@ def __init__(self, data):
113111 sequence of values
114112 """
115113 self .seq , self .locs = [], []
116- self ._set_seq (data )
117- self ._set_locs (0 )
114+ self ._set_seq_locs (data , 0 )
118115
119116 def update (self , new_data ):
120- self . _set_seq ( new_data )
121- value = max (self .locs )
122- self ._set_locs ( value + 1 )
117+ # so as not to conflict with spdict
118+ value = max (max ( self .locs ) + 1 , 0 )
119+ self ._set_seq_locs ( new_data , value )
123120
124- def _set_seq (self , data ):
125- #magic to make it work under np1.6
121+ def _set_seq_locs (self , data , value ):
122+ # magic to make it work under np1.6
126123 strdata = to_array (data )
127124 # np.unique makes dateframes work
128- for d in np .unique (strdata ):
129- if d not in self .seq :
130- self .seq .append (convert_to_string (d ))
131- self .locs .append (UnitData .LOWER_BOUND )
132-
133- def _set_locs (self , value ):
134- for i , s in enumerate (self .seq ):
135- if s in UnitData .spdict .keys ():
136- self .locs [i ] = UnitData .spdict [s ]
137- elif self .locs [i ] == UnitData .LOWER_BOUND :
138- self .locs [i ] = value
125+ new_s = [d for d in np .unique (strdata ) if d not in self .seq ]
126+ for ns in new_s :
127+ self .seq .append (convert_to_string (ns ))
128+ if ns in UnitData .spdict .keys ():
129+ self .locs .append (UnitData .spdict [ns ])
130+ else :
131+ self .locs .append (value )
139132 value += 1
140133
141-
142134# Connects the convertor to matplotlib
143135units .registry [str ] = StrCategoryConverter ()
144136units .registry [bytes ] = StrCategoryConverter ()
0 commit comments