@@ -494,8 +494,10 @@ def test_flatiter():
494
494
495
495
496
496
def test_reshape2d ():
497
+
497
498
class dummy ():
498
499
pass
500
+
499
501
xnew = cbook ._reshape_2D ([], 'x' )
500
502
assert np .shape (xnew ) == (1 , 0 )
501
503
@@ -517,6 +519,41 @@ class dummy():
517
519
xnew = cbook ._reshape_2D (x , 'x' )
518
520
assert np .shape (xnew ) == (5 , 3 )
519
521
522
+ # Now test with a list of lists with different lengths, which means the
523
+ # array will internally be converted to a 1D object array of lists
524
+ x = [[1 , 2 , 3 ], [3 , 4 ], [2 ]]
525
+ xnew = cbook ._reshape_2D (x , 'x' )
526
+ assert isinstance (xnew , list )
527
+ assert isinstance (xnew [0 ], np .ndarray ) and xnew [0 ].shape == (3 ,)
528
+ assert isinstance (xnew [1 ], np .ndarray ) and xnew [1 ].shape == (2 ,)
529
+ assert isinstance (xnew [2 ], np .ndarray ) and xnew [2 ].shape == (1 ,)
530
+
531
+ # We now need to make sure that this works correctly for Numpy subclasses
532
+ # where iterating over items can return subclasses too, which may be
533
+ # iterable even if they are scalars. To emulate this, we make a Numpy
534
+ # array subclass that returns Numpy 'scalars' when iterating or accessing
535
+ # values, and these are technically iterable if checking for example
536
+ # isinstance(x, collections.abc.Iterable).
537
+
538
+ class ArraySubclass (np .ndarray ):
539
+
540
+ def __iter__ (self ):
541
+ for value in super ().__iter__ ():
542
+ yield np .array (value )
543
+
544
+ def __getitem__ (self , item ):
545
+ return np .array (super ().__getitem__ (item ))
546
+
547
+ v = np .arange (10 , dtype = float )
548
+ x = ArraySubclass ((10 ,), dtype = float , buffer = v .data )
549
+
550
+ xnew = cbook ._reshape_2D (x , 'x' )
551
+
552
+ # We check here that the array wasn't split up into many individual
553
+ # ArraySubclass, which is what used to happen due to a bug in _reshape_2D
554
+ assert len (xnew ) == 1
555
+ assert isinstance (xnew [0 ], ArraySubclass )
556
+
520
557
521
558
def test_contiguous_regions ():
522
559
a , b , c = 3 , 4 , 5
0 commit comments