Skip to content

Simplify implementation of is_numlike & is_string_like. #7996

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions doc/api/api_changes/2017-01-30-AL_is_numlike_stringlike.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
`cbook.is_numlike` and `cbook.is_string_like` only perform an instance check
````````````````````````````````````````````````````````````````````````````

`cbook.is_numlike` and `cbook.is_string_like` now only check that
their argument is an instance of ``(numbers.Number, np.Number)`` and
``(six.string_types, np.str_, np.unicode_)`` respectively. In particular, this
means that arrays are now never num-like or string-like regardless of their
dtype.
23 changes: 4 additions & 19 deletions lib/matplotlib/cbook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import gzip
import io
import locale
import numbers
import os
import re
import sys
Expand Down Expand Up @@ -495,19 +496,8 @@ def iterable(obj):

def is_string_like(obj):
"""Return True if *obj* looks like a string"""
if isinstance(obj, six.string_types):
return True
# numpy strings are subclass of str, ma strings are not
if isinstance(obj, np.ma.MaskedArray):
if obj.ndim == 0 and obj.dtype.kind in 'SU':
return True
else:
return False
try:
obj + ''
except:
return False
return True
# (np.str_ == np.unicode_ on Py3).
return isinstance(obj, (six.string_types, np.str_, np.unicode_))


def is_sequence_of_strings(obj):
Expand Down Expand Up @@ -560,12 +550,7 @@ def is_scalar(obj):

def is_numlike(obj):
"""return true if *obj* looks like a number"""
try:
obj + 1
except:
return False
else:
return True
return isinstance(obj, (numbers.Number, np.number))


def to_filehandle(fname, flag='rU', return_opened=False):
Expand Down
3 changes: 0 additions & 3 deletions lib/matplotlib/tests/test_cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ def test_is_string_like():
y = np.array(y)
assert not cbook.is_string_like(y)

y = np.array(y, dtype=object)
assert cbook.is_string_like(y)


def test_is_sequence_of_strings():
y = ['a', 'b', 'c']
Expand Down