10000 Merge pull request #6148 from has2k1/fix-pandas-indexing · andnovar/matplotlib@668bd02 · GitHub
[go: up one dir, main page]

Skip to content

Commit 668bd02

Browse files
committed
Merge pull request matplotlib#6148 from has2k1/fix-pandas-indexing
Fix: Pandas indexing Error in collections
2 parents 7f21cc5 + 3418f46 commit 668bd02

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
@@ -151,7 +151,7 @@ def _get_value(val):
151151
except TypeError:
152152
if cbook.iterable(val) and len(val):
153153
try:
154-
float(val[0])
154+
float(cbook.safe_first_element(val))
155155
except (TypeError, ValueError):
156156
pass # raise below
157157
else:
@@ -164,7 +164,7 @@ def _get_bool(val):
164164
if not cbook.iterable(val):
165165
val = (val,)
166166
try:
167-
bool(val[0])
167+
bool(cbook.safe_first_element(val))
168168
except (TypeError, IndexError):
169169
raise TypeError('val must be a bool or nonzero sequence of them')
170170
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)
0