From 3418f4650fb8187c4744a7bf0f01b2f077d3879f Mon Sep 17 00:00:00 2001 From: Hassan Kibirige Date: Sat, 12 Mar 2016 13:31:12 -0600 Subject: [PATCH] Fix: Pandas indexing Error in collections Specifically, when creating collection objects, `linewidths` and `antialiaseds` can be non zero indexed pandas series. Related to: PR #5556 --- lib/matplotlib/collections.py | 4 ++-- lib/matplotlib/tests/test_collections.py | 22 +++++++++++++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/collections.py b/lib/matplotlib/collections.py index 2a33ba21b374..9784e0b90b70 100644 --- a/lib/matplotlib/collections.py +++ b/lib/matplotlib/collections.py @@ -151,7 +151,7 @@ def _get_value(val): except TypeError: if cbook.iterable(val) and len(val): try: - float(val[0]) + float(cbook.safe_first_element(val)) except (TypeError, ValueError): pass # raise below else: @@ -164,7 +164,7 @@ def _get_bool(val): if not cbook.iterable(val): val = (val,) try: - bool(val[0]) + bool(cbook.safe_first_element(val)) except (TypeError, IndexError): raise TypeError('val must be a bool or nonzero sequence of them') return val diff --git a/lib/matplotlib/tests/test_collections.py b/lib/matplotlib/tests/test_collections.py index 3001147b1841..23cc507d0447 100644 --- a/lib/matplotlib/tests/test_collections.py +++ b/lib/matplotlib/tests/test_collections.py @@ -11,11 +11,12 @@ from nose.tools import assert_equal import numpy as np from numpy.testing import assert_array_equal, assert_array_almost_equal +from nose.plugins.skip import SkipTest import matplotlib.pyplot as plt import matplotlib.collections as mcollections import matplotlib.transforms as mtransforms -from matplotlib.collections import EventCollection +from matplotlib.collections import Collection, EventCollection from matplotlib.testing.decorators import cleanup, image_comparison @@ -617,6 +618,25 @@ def test_size_in_xy(): ax.set_ylim(0, 30) +def test_pandas_indexing(): + try: + import pandas as pd + except ImportError: + raise SkipTest("Pandas not installed") + + # Should not fail break when faced with a + # non-zero indexed series + index = [11, 12, 13] + ec = fc = pd.Series(['red', 'blue', 'green'], index=index) + lw = pd.Series([1, 2, 3], index=index) + aa = pd.Series([True, False, True], index=index) + + Collection(edgecolors=ec) + Collection(facecolors=fc) + Collection(linewidths=lw) + Collection(antialiaseds=aa) + + if __name__ == '__main__': import nose nose.runmodule(argv=['-s', '--with-doctest'], exit=False)