Skip to content
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

cbook.is_sequence_of_strings knows string objects #4358

Merged
merged 1 commit into from Apr 30, 2015
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions doc/users/whats_new/cbook.rst
@@ -0,0 +1,18 @@
cbook.is_sequence_of_strings recognizes string objects
``````````````````````````````````````````````````````

This is primarily how pandas stores a sequence of strings.

import pandas as pd
import matplotlib.cbook as cbook

a = np.array(['a', 'b', 'c'])
print(cbook.is_sequence_of_strings(a)) # True

a = np.array(['a', 'b', 'c'], dtype=object)
print(cbook.is_sequence_of_strings(a)) # True

s = pd.Series(['a', 'b', 'c'])
print(cbook.is_sequence_of_strings(s)) # True

Previously, the last two prints returned false.
8 changes: 6 additions & 2 deletions lib/matplotlib/cbook.py
Expand Up @@ -783,8 +783,12 @@ def is_sequence_of_strings(obj):
"""
if not iterable(obj):
return False
if is_string_like(obj):
return False
if is_string_like(obj) and not isinstance(obj, np.ndarray):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will not catch new pd.Series as they are no longer ndarray sub-classes

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think something like

if is_string_like(obj):
    # remove possible pandas
    obj_arr = np.asarray(obj)
    if not is_string_like(obj_arr):
        return False

or

if is_string_like(obj) and not isinstance(obj, np.ndarray):
    try:
        obj = s.values
    except AttributeError:
        # not pandas
        return False

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reference

In [43]: isinstance(s, np.ndarray)
Out[43]: False

In [44]: type(s)
Out[44]: pandas.core.series.Series

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I'll fix. I now see that Pandas has moved 3 major versions since I encountered this. I need to do a better job keeping all my virtual environments up-to-date.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does is_string_like(series of str) do? Does it loop through the array checking to see if each element is a str?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_string_like(obj) checks if string concatenation is possible. i.e If you can add a string to it, then it is "string like".

is_string_like(series of str) is indeed "string like".

Defn: is_string_like

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we can not import pandas directly in core matplotlib for both technical
and policy reasons.

On Tue, Apr 28, 2015, 11:31 Phillip Cloud notifications@github.com wrote:

In lib/matplotlib/cbook.py
#4358 (comment):

@@ -783,8 +783,12 @@ def is_sequence_of_strings(obj):
"""
if not iterable(obj):
return False

  • if is_string_like(obj):
  •    return False
    
  • if is_string_like(obj) and not isinstance(obj, np.ndarray):

What does is_string_like(series of str) do? Does it loop through the
array checking to see if each element is a str?


Reply to this email directly or view it on GitHub
https://github.com/matplotlib/matplotlib/pull/4358/files#r29254616.

try:
obj = obj.values
except AttributeError:
# not pandas
return False
for o in obj:
if not is_string_like(o):
return False
Expand Down
17 changes: 17 additions & 0 deletions lib/matplotlib/tests/test_cbook.py
Expand Up @@ -26,6 +26,23 @@ def test_is_string_like():
assert cbook.is_string_like("hello world")
assert_equal(cbook.is_string_like(10), False)

y = ['a', 'b', 'c']
assert_equal(cbook.is_string_like(y), False)

y = np.array(y)
assert_equal(cbook.is_string_like(y), False)

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


def test_is_sequence_of_strings():
y = ['a', 'b', 'c']
assert cbook.is_sequence_of_strings(y)

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


def test_restrict_dict():
d = {'foo': 'bar', 1: 2}
Expand Down