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

Skip to content

Commit 1d7f0d9

Browse files
committed
Merge pull request #6148 from has2k1/fix-pandas-indexing
Fix: Pandas indexing Error in collections
1 parent eadeb1a commit 1d7f0d9

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

lib/matplotlib/collections.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def _get_value(val):
146146
except TypeError:
147147
if cbook.iterable(val) and len(val):
148148
try:
149-
float(val[0])
149+
float(cbook.safe_first_element(val))
150150
except (TypeError, ValueError):
151151
pass # raise below
152152
else:
@@ -159,7 +159,7 @@ def _get_bool(val):
159159
if not cbook.iterable(val):
160160
val = (val,)
161161
try:
162-
bool(val[0])
162+
bool(cbook.safe_first_element(val))
163163
except (TypeError, IndexError):
164164
raise TypeError('val must be a bool or nonzero sequence of them')
165165
return val

lib/matplotlib/tests/test_collections.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
from nose.tools import assert_equal
1212
import numpy as np
1313
from numpy.testing import assert_array_equal, assert_array_almost_equal
14+
from nose.plugins.skip import SkipTest
1415

1516
import matplotlib.pyplot as plt
1617
import matplotlib.collections as mcollections
1718
import matplotlib.transforms as mtransforms
18-
from matplotlib.collections import EventCollection
19+
from matplotlib.collections import Collection, EventCollection
1920
from matplotlib.testing.decorators import cleanup, image_comparison
2021

2122

@@ -617,6 +618,25 @@ def test_size_in_xy():
617618
ax.set_ylim(0, 30)
618619

619620

621+
def test_pandas_indexing():
622+
try:
623+
import pandas as pd
624+
except ImportError:
625+
raise SkipTest("Pandas not installed")
626+
627+
# Should not fail break when faced with a
628+
# non-zero indexed series
629+
index = [11, 12, 13]
630+
ec = fc = pd.Series(['red', 'blue', 'green'], index=index)
631+
lw = pd.Series([1, 2, 3], index=index)
632+
aa = pd.Series([True, False, True], index=index)
633+
634+
Collection(edgecolors=ec)
635+
Collection(facecolors=fc)
636+
Collection(linewidths=lw)
637+
Collection(antialiaseds=aa)
638+
639+
620640
if __name__ == '__main__':
621641
import nose
622642
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
 (0)