@@ -1032,7 +1032,12 @@ def _combine_masks(*args):
10321032 else :
10331033 if isinstance (x , np .ma .MaskedArray ) and x .ndim > 1 :
10341034 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 )
10361041 if x .ndim == 1 :
10371042 x = safe_masked_invalid (x )
10381043 seqlist [i ] = True
@@ -1366,24 +1371,48 @@ def _reshape_2D(X, name):
13661371 Use Fortran ordering to convert ndarrays and lists of iterables to lists of
13671372 1D arrays.
13681373
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*.
13721377
13731378 *name* is used to generate the error message for invalid inputs.
13741379 """
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.
13771396 if len (X ) == 0 :
13781397 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 :
13801411 # 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 )]
13851413 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
13871416
13881417
13891418def violin_stats (X , method , points = 100 , quantiles = None ):
@@ -1448,10 +1477,10 @@ def violin_stats(X, method, points=100, quantiles=None):
14481477 quantiles = _reshape_2D (quantiles , "quantiles" )
14491478 # Else, mock quantiles if is none or empty
14501479 else :
1451- quantiles = [[]] * np . shape (X )[ 0 ]
1480+ quantiles = [[]] * len (X )
14521481
14531482 # quantiles should has the same size as dataset
1454- if np . shape (X )[: 1 ] != np . shape (quantiles )[: 1 ] :
1483+ if len (X ) != len (quantiles ):
14551484 raise ValueError ("List of violinplot statistics and quantiles values"
14561485 " must have the same length" )
14571486
@@ -1626,8 +1655,15 @@ def index_of(y):
16261655 try :
16271656 return y .index .values , y .values
16281657 except AttributeError :
1658+ pass
1659+ try :
16291660 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 :
16301665 return np .arange (y .shape [0 ], dtype = float ), y
1666+ raise ValueError ('Input could not be cast to an at-least-1D NumPy array' )
16311667
16321668
16331669def safe_first_element (obj ):
0 commit comments