@@ -1322,18 +1322,42 @@ def _reshape_2D(X, name):
1322
1322
1323
1323
*name* is used to generate the error message for invalid inputs.
1324
1324
"""
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 ))
1325
+ # Iterate over columns for ndarrays.
1326
+ if isinstance (X , np .ndarray ):
1327
+ X = X .T
1328
+
1329
+ if len (X ) == 0 :
1330
+ return [[]]
1331
+ elif X .ndim == 1 and np .ndim (X [0 ]) == 0 :
1332
+ # 1D array of scalars: directly return it.
1333
+ return [X ]
1334
+ elif X .ndim in [1 , 2 ]:
1335
+ # 2D array, or 1D array of iterables: flatten them first.
1336
+ return [np .reshape (x , - 1 ) for x in X ]
1337
+ else :
1338
+ raise ValueError ("{} must have 2 or fewer dimensions" .format (name ))
1339
+
1340
+ # Iterate over rows for non-ndarrays.
1327
1341
if len (X ) == 0 :
1328
1342
return [[]]
1329
- elif X .ndim == 1 and np .ndim (X [0 ]) == 0 :
1343
+
1344
+ result = []
1345
+ is_1d = True
1346
+ for xi in X :
1347
+ xi = np .asarray (xi )
1348
+ nd = np .ndim (xi )
1349
+ if nd > 1 :
1350
+ raise ValueError ("{} must have 2 or fewer dimensions" .format (name ))
1351
+ elif nd == 1 and len (xi ) != 1 :
1352
+ is_1d = False
1353
+ result .append (xi .reshape (- 1 ))
1354
+
1355
+ if is_1d :
1330
1356
# 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 ]
1357
+ return [np .reshape (result , - 1 )]
1335
1358
else :
1336
- raise ValueError ("{} must have 2 or fewer dimensions" .format (name ))
1359
+ # 2D array, or 1D array of iterables: use flattened version.
1360
+ return result
1337
1361
1338
1362
1339
1363
def violin_stats (X , method , points = 100 , quantiles = None ):
0 commit comments