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

Skip to content

Fix: pandas series of strings #6158

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
Mar 16, 2016
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
11 changes: 11 additions & 0 deletions lib/matplotlib/cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,17 @@ def is_sequence_of_strings(obj):
return True


def is_hashable(obj):
Copy link
Member

Choose a reason for hiding this comment

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

Does isinstance(obj, collections.Hashable) not work?

Copy link
Member

Choose a reason for hiding this comment

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

That only works for built-in types or types that explicitly use the collections.Hashable abc. This implementation is more "duck-typing" style and therefore includes more things.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

"""
Returns true if *obj* can be hashed
"""
try:
hash(obj)
except TypeError:
return False
return True


def is_writable_file_like(obj):
'return true if *obj* looks like a file object with a *write* method'
return hasattr(obj, 'write') and six.callable(obj.write)
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ def set_linestyle(self, ls):
The line style.
"""
try:
if cbook.is_string_like(ls):
if cbook.is_string_like(ls) and cbook.is_hashable(ls):
ls = cbook.ls_mapper.get(ls, ls)
dashes = [mlines.get_dash_pattern(ls)]
elif cbook.iterable(ls):
Expand Down
3 changes: 2 additions & 1 deletion lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,8 @@ def __init__(self, colors, name='from_list', N=None):
if N is None:
N = len(self.colors)
else:
if cbook.is_string_like(self.colors):
if (cbook.is_string_like(self.colors) and
cbook.is_hashable(self.colors)):
self.colors = [self.colors] * N
self.monochrome = True
elif cbook.iterable(self.colors):
Expand Down
8 changes: 8 additions & 0 deletions lib/matplotlib/tests/test_cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ def test_is_sequence_of_strings():
assert cbook.is_sequence_of_strings(y)


def test_is_hashable():
s = 'string'
assert cbook.is_hashable(s)

lst = ['list', 'of', 'stings']
assert not cbook.is_hashable(lst)


def test_restrict_dict():
d = {'foo': 'bar', 1: 2}
d1 = cbook.restrict_dict(d, ['foo', 1])
Expand Down
2 changes: 2 additions & 0 deletions lib/matplotlib/tests/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,11 +629,13 @@ def test_pandas_indexing():
index = [11, 12, 13]
ec = fc = pd.Series(['red', 'blue', 'green'], index=index)
lw = pd.Series([1, 2, 3], index=index)
ls = pd.Series(['solid', 'dashed', 'dashdot'], index=index)
aa = pd.Series([True, False, True], index=index)

Collection(edgecolors=ec)
Collection(facecolors=fc)
Collection(linewidths=lw)
Collection(linestyles=ls)
Collection(antialiaseds=aa)


Expand Down
18 changes: 18 additions & 0 deletions lib/matplotlib/tests/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
from distutils.version import LooseVersion as V

from nose.tools import assert_raises, assert_equal, assert_true
from nose.tools import assert_sequence_equal

import numpy as np
from numpy.testing.utils import assert_array_equal, assert_array_almost_equal
from nose.plugins.skip import SkipTest

import matplotlib.colors as mcolors
import matplotlib.cm as cm
Expand Down Expand Up @@ -563,6 +565,22 @@ def _azimuth2math(azimuth, elevation):
return theta, phi


def test_pandas_iterable():
try:
import pandas as pd
except ImportError:
raise SkipTest("Pandas not installed")

# Using a list or series yields equivalent
# color maps, i.e the series isn't seen as
# a single color
lst = ['red', 'blue', 'green']
s = pd.Series(lst)
cm1 = mcolors.ListedColormap(lst, N=5)
cm2 = mcolors.ListedColormap(s, N=5)
assert_sequence_equal(cm1.colors, cm2.colors)


if __name__ == '__main__':
import nose
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)