@@ -989,7 +989,12 @@ def _combine_masks(*args):
989989 else :
990990 if isinstance (x , np .ma .MaskedArray ) and x .ndim > 1 :
991991 raise ValueError ("Masked arrays must be 1-D" )
992- x = np .asanyarray (x )
992+ try :
993+ x = np .asanyarray (x )
994+ except (np .VisibleDeprecationWarning , ValueError ):
995+ # NumPy 1.19 raises a warning about ragged arrays, but we want
996+ # to accept basically anything here.
997+ x = np .asanyarray (x , dtype = object )
993998 if x .ndim == 1 :
994999 x = safe_masked_invalid (x )
9951000 seqlist [i ] = True
@@ -1316,24 +1321,48 @@ def _reshape_2D(X, name):
13161321 Use Fortran ordering to convert ndarrays and lists of iterables to lists of
13171322 1D arrays.
13181323
1319- Lists of iterables are converted by applying `np.asarray ` to each of their
1320- elements. 1D ndarrays are returned in a singleton list containing them.
1321- 2D ndarrays are converted to the list of their *columns*.
1324+ Lists of iterables are converted by applying `np.asanyarray ` to each of
1325+ their elements. 1D ndarrays are returned in a singleton list containing
1326+ them. 2D ndarrays are converted to the list of their *columns*.
13221327
13231328 *name* is used to generate the error message for invalid inputs.
13241329 """
1325- # Iterate over columns for ndarrays, over rows otherwise.
1326- X = np .atleast_1d (X .T if isinstance (X , np .ndarray ) else np .asarray (X ))
1330+ # Iterate over columns for ndarrays.
1331+ if isinstance (X , np .ndarray ):
1332+ X = X .T
1333+
1334+ if len (X ) == 0 :
1335+ return [[]]
1336+ elif X .ndim == 1 and np .ndim (X [0 ]) == 0 :
1337+ # 1D array of scalars: directly return it.
1338+ return [X ]
1339+ elif X .ndim in [1 , 2 ]:
1340+ # 2D array, or 1D array of iterables: flatten them first.
1341+ return [np .reshape (x , - 1 ) for x in X ]
1342+ else :
1343+ raise ValueError (f'{ name } must have 2 or fewer dimensions' )
1344+
1345+ # Iterate over list of iterables.
13271346 if len (X ) == 0 :
13281347 return [[]]
1329- elif X .ndim == 1 and np .ndim (X [0 ]) == 0 :
1348+
1349+ result = []
1350+ is_1d = True
1351+ for xi in X :
1352+ xi = np .asanyarray (xi )
1353+ nd = np .ndim (xi )
1354+ if nd > 1 :
1355+ raise ValueError (f'{ name } must have 2 or fewer dimensions' )
1356+ elif nd == 1 and len (xi ) != 1 :
1357+ is_1d = False
1358+ result .append (xi .reshape (- 1 ))
1359+
1360+ if is_1d :
13301361 # 1D array of scalars: directly return it.
1331- return [X ]
1332- elif X .ndim in [1 , 2 ]:
1333- # 2D array, or 1D array of iterables: flatten them first.
1334- return [np .reshape (x , - 1 ) for x in X ]
1362+ return [np .reshape (result , - 1 )]
13351363 else :
1336- raise ValueError ("{} must have 2 or fewer dimensions" .format (name ))
1364+ # 2D array, or 1D array of iterables: use flattened version.
1365+ return result
13371366
13381367
13391368def violin_stats (X , method , points = 100 , quantiles = None ):
@@ -1399,10 +1428,10 @@ def violin_stats(X, method, points=100, quantiles=None):
13991428 quantiles = _reshape_2D (quantiles , "quantiles" )
14001429 # Else, mock quantiles if is none or empty
14011430 else :
1402- quantiles = [[]] * np . shape (X )[ 0 ]
1431+ quantiles = [[]] * len (X )
14031432
14041433 # quantiles should has the same size as dataset
1405- if np . shape (X )[: 1 ] != np . shape (quantiles )[: 1 ] :
1434+ if len (X ) != len (quantiles ):
14061435 raise ValueError ("List of violinplot statistics and quantiles values"
14071436 " must have the same length" )
14081437
@@ -1577,8 +1606,15 @@ def index_of(y):
15771606 try :
15781607 return y .index .values , y .values
15791608 except AttributeError :
1609+ pass
1610+ try :
15801611 y = _check_1d (y )
1612+ except (np .VisibleDeprecationWarning , ValueError ):
1613+ # NumPy 1.19 will warn on ragged input, and we can't actually use it.
1614+ pass
1615+ else :
15811616 return np .arange (y .shape [0 ], dtype = float ), y
1617+ raise ValueError ('Input could not be cast to an at-least-1D NumPy array' )
15821618
15831619
15841620def safe_first_element (obj ):
0 commit comments