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

Skip to content

Commit 17a7c54

Browse files
committed
FIX: one-loop version of categorical logging call
1 parent 5c4c41d commit 17a7c54

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

lib/matplotlib/category.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -181,20 +181,18 @@ def __init__(self, data=None):
181181
self.update(data)
182182

183183
@staticmethod
184-
def _strs_are_convertible(vals):
184+
def _str_is_convertible(val):
185185
"""
186-
Helper method to see if list of strings can all be cast to float or
186+
Helper method to see if a string can be cast to float or
187187
parsed as date.
188188
"""
189-
190-
for val in vals:
189+
try:
190+
float(val)
191+
except ValueError:
191192
try:
192-
float(val)
193+
dateutil.parser.parse(val)
193194
except ValueError:
194-
try:
195-
dateutil.parser.parse(val)
196-
except ValueError:
197-
return False
195+
return False
198196
return True
199197

200198
def update(self, data):
@@ -212,18 +210,22 @@ def update(self, data):
212210
"""
213211
data = np.atleast_1d(np.array(data, dtype=object))
214212

213+
# check if convertable to number:
214+
convertable = True
215215
for val in OrderedDict.fromkeys(data):
216216
# OrderedDict just iterates over unique values in data.
217217
if not isinstance(val, (str, bytes)):
218218
raise TypeError("{val!r} is not a string".format(val=val))
219+
if convertable:
220+
# this will only be called so long as convertable is True.
221+
convertable = self._str_is_convertible(val)
219222
if val not in self._mapping:
220223
self._mapping[val] = next(self._counter)
221-
# check if we can convert all strings to number or date...
222-
if self._strs_are_convertible(data):
223-
_log.info('using category units to plot a list of '
224-
'strings that is a;; floats or parsable as dates. '
225-
'If you do not mean these to be categories, cast '
226-
'to the approriate data type before plotting.')
224+
if convertable:
225+
_log.info('Using categrocical units to plot a list of strings '
226+
'that are all parsable as floats or dates. If these '
227+
'strings should be plotted as numbers, cast to the '
228+
'approriate data type before plotting.')
227229

228230

229231
# Register the converter with Matplotlib's unit framework

0 commit comments

Comments
 (0)