@@ -1032,7 +1032,12 @@ def _combine_masks(*args):
1032
1032
else :
1033
1033
if isinstance (x , np .ma .MaskedArray ) and x .ndim > 1 :
1034
1034
raise ValueError ("Masked arrays must be 1-D" )
1035
- x = np .asanyarray (x )
1035
+ try :
1036
+ x = np .asanyarray (x )
1037
+ except (np .VisibleDeprecationWarning , ValueError ):
1038
+ # NumPy 1.19 raises a warning about ragged arrays, but we want
1039
+ # to accept basically anything here.
1040
+ x = np .asanyarray (x , dtype = object )
1036
1041
if x .ndim == 1 :
1037
1042
x = safe_masked_invalid (x )
1038
1043
seqlist [i ] = True
@@ -1366,24 +1371,48 @@ def _reshape_2D(X, name):
1366
1371
Use Fortran ordering to convert ndarrays and lists of iterables to lists of
1367
1372
1D arrays.
1368
1373
1369
- Lists of iterables are converted by applying `np.asarray ` to each of their
1370
- elements. 1D ndarrays are returned in a singleton list containing them.
1371
- 2D ndarrays are converted to the list of their *columns*.
1374
+ Lists of iterables are converted by applying `np.asanyarray ` to each of
1375
+ their elements. 1D ndarrays are returned in a singleton list containing
1376
+ them. 2D ndarrays are converted to the list of their *columns*.
1372
1377
1373
1378
*name* is used to generate the error message for invalid inputs.
1374
1379
"""
1375
- # Iterate over columns for ndarrays, over rows otherwise.
1376
- X = np .atleast_1d (X .T if isinstance (X , np .ndarray ) else np .asarray (X ))
1380
+ # Iterate over columns for ndarrays.
1381
+ if isinstance (X , np .ndarray ):
1382
+ X = X .T
1383
+
1384
+ if len (X ) == 0 :
1385
+ return [[]]
1386
+ elif X .ndim == 1 and np .ndim (X [0 ]) == 0 :
1387
+ # 1D array of scalars: directly return it.
1388
+ return [X ]
1389
+ elif X .ndim in [1 , 2 ]:
1390
+ # 2D array, or 1D array of iterables: flatten them first.
1391
+ return [np .reshape (x , - 1 ) for x in X ]
1392
+ else :
1393
+ raise ValueError (f'{ name } must have 2 or fewer dimensions' )
1394
+
1395
+ # Iterate over list of iterables.
1377
1396
if len (X ) == 0 :
1378
1397
return [[]]
1379
- elif X .ndim == 1 and np .ndim (X [0 ]) == 0 :
1398
+
1399
+ result = []
1400
+ is_1d = True
1401
+ for xi in X :
1402
+ xi = np .asanyarray (xi )
1403
+ nd = np .ndim (xi )
1404
+ if nd > 1 :
1405
+ raise ValueError (f'{ name } must have 2 or fewer dimensions' )
1406
+ elif nd == 1 and len (xi ) != 1 :
1407
+ is_1d = False
1408
+ result .append (xi .reshape (- 1 ))
1409
+
1410
+ if is_1d :
1380
1411
# 1D array of scalars: directly return it.
1381
- return [X ]
1382
- elif X .ndim in [1 , 2 ]:
1383
- # 2D array, or 1D array of iterables: flatten them first.
1384
- return [np .reshape (x , - 1 ) for x in X ]
1412
+ return [np .reshape (result , - 1 )]
1385
1413
else :
1386
- raise ValueError ("{} must have 2 or fewer dimensions" .format (name ))
1414
+ # 2D array, or 1D array of iterables: use flattened version.
1415
+ return result
1387
1416
1388
1417
1389
1418
def violin_stats (X , method , points = 100 , quantiles = None ):
@@ -1448,10 +1477,10 @@ def violin_stats(X, method, points=100, quantiles=None):
1448
1477
quantiles = _reshape_2D (quantiles , "quantiles" )
1449
1478
# Else, mock quantiles if is none or empty
1450
1479
else :
1451
- quantiles = [[]] * np . shape (X )[ 0 ]
1480
+ quantiles = [[]] * len (X )
1452
1481
1453
1482
# quantiles should has the same size as dataset
1454
- if np . shape (X )[: 1 ] != np . shape (quantiles )[: 1 ] :
1483
+ if len (X ) != len (quantiles ):
1455
1484
raise ValueError ("List of violinplot statistics and quantiles values"
1456
1485
" must have the same length" )
1457
1486
@@ -1626,8 +1655,15 @@ def index_of(y):
1626
1655
try :
1627
1656
return y .index .values , y .values
1628
1657
except AttributeError :
1658
+ pass
1659
+ try :
1629
1660
y = _check_1d (y )
1661
+ except (np .VisibleDeprecationWarning , ValueError ):
1662
+ # NumPy 1.19 will warn on ragged input, and we can't actually use it.
1663
+ pass
1664
+ else :
1630
1665
return np .arange (y .shape [0 ], dtype = float ), y
1666
+ raise ValueError ('Input could not be cast to an at-least-1D NumPy array' )
1631
1667
1632
1668
1633
1669
def safe_first_element (obj ):
0 commit comments