@@ -372,6 +372,26 @@ def intersection(*seqs):
372
372
return type (seqs [0 ])(list (result ))
373
373
374
374
375
+ def _as_1d_array (values , dtype = 'object' ):
376
+ """
377
+ Transform any list-like object in a 1-dimensional numpy array.
378
+
379
+ Parameters
380
+ ----------
381
+ values : any iterable which has a len()
382
+ dtype : dtype, default 'object'
383
+
384
+ Returns
385
+ -------
386
+ 1-dimensional numpy array
387
+ """
388
+ # Making a 1D array that safely contains list-likes is a bit tricky
389
+ # in numpy, leading to the following
390
+ result = np .empty (len (values ), dtype = dtype )
391
+ result [:] = values
392
+ return result
393
+
394
+
375
395
def _asarray_tuplesafe (values , dtype = None ):
376
396
from pandas .core .index import Index
377
397
@@ -381,25 +401,17 @@ def _asarray_tuplesafe(values, dtype=None):
381
401
return values .values
382
402
383
403
if isinstance (values , list ) and dtype in [np .object_ , object ]:
384
- return lib . list_to_object_array (values )
404
+ return _as_1d_array (values )
385
405
386
406
result = np .asarray (values , dtype = dtype )
387
407
388
408
if issubclass (result .dtype .type , compat .string_types ):
389
409
result = np .asarray (values , dtype = object )
390
410
391
411
if result .ndim == 2 :
392
- if isinstance (values , list ):
393
- return lib .list_to_object_array (values )
394
- else :
395
- # Making a 1D array that safely contains tuples is a bit tricky
396
- # in numpy, leading to the following
397
- try :
398
- result = np .empty (len (values ), dtype = object )
399
- result [:] = values
400
- except ValueError :
401
- # we have a list-of-list
402
- result [:] = [tuple (x ) for x in values ]
412
+ if hasattr (values , '__array__' ):
413
+ values = [tuple (x ) for x in values ]
414
+ result = _as_1d_array (values )
403
415
404
416
return result
405
417
0 commit comments