8000 FIX: support np.flatiter as input again · matplotlib/matplotlib@8d03299 · GitHub
[go: up one dir, main page]

Skip to content
65F9

Commit 8d03299

Browse files
committed
FIX: support np.flatiter as input again
This was unintentionally broken by bdaaf59 which went in as part of PR #5556 which was fixing support for pandas series.
1 parent b2b37fb commit 8d03299

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

lib/matplotlib/cbook.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2544,6 +2544,15 @@ def index_of(y):
25442544

25452545
def safe_first_element(obj):
25462546
if isinstance(obj, collections.Iterator):
2547+
# needed to accept `array.flat` as input.
2548+
# np.flatiter reports as an instance of collections.Iterator
2549+
# but can still be indexed via [].
2550+
# This has the side effect of re-setting the iterator, but
2551+
# that is acceptable.
2552+
try:
2553+
return obj[0]
2554+
except TypeError:
2555+
pass
25472556
raise RuntimeError("matplotlib does not support generators "
25482557
"as input")
25492558
return next(iter(obj))

lib/matplotlib/tests/test_cbook.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,3 +499,15 @@ class dummy():
499499
base_set = mapping[ref(objs[0])]
500500
for o in objs[1:]:
501501
assert mapping[ref(o)] is base_set
502+
503+
504+
def test_flatiter():
505+
x = np.arange(5)
506+
it = x.flat
507+
assert 0 == next(it)
508+
assert 1 == next(it)
509+
ret = cbook.safe_first_element(it)
510+
assert ret == 0
511+
512+
assert 0 == next(it)
513+
assert 1 == next(it)

0 commit comments

Comments
 (0)
0