@@ -449,6 +449,11 @@ def sanitize_array(
449
449
# extract ndarray or ExtensionArray, ensure we have no PandasArray
450
450
data = extract_array (data , extract_numpy = True )
451
451
452
+ if isinstance (data , np .ndarray ) and data .ndim == 0 :
453
+ if dtype is None :
454
+ dtype = data .dtype
455
+ data = lib .item_from_zerodim (data )
456
+
452
457
# GH#846
453
458
if isinstance (data , np .ndarray ):
454
459
@@ -462,7 +467,7 @@ def sanitize_array(
462
467
else :
463
468
subarr = np .array (data , copy = False )
464
469
else :
465
- # we will try to copy be -definition here
470
+ # we will try to copy by -definition here
466
471
subarr = _try_cast (data , dtype , copy , raise_cast_failure )
467
472
468
473
elif isinstance (data , ABCExtensionArray ):
@@ -491,30 +496,16 @@ def sanitize_array(
491
496
# GH#16804
492
497
arr = np .arange (data .start , data .stop , data .step , dtype = "int64" )
493
498
subarr = _try_cast (arr , dtype , copy , raise_cast_failure )
494
- elif lib .is_scalar (data ) and index is not None and dtype is not None :
499
+
500
+ elif not is_list_like (data ):
501
+ if index is None :
502
+ raise ValueError ("index must be specified when data is not list-like" )
495
503
subarr = construct_1d_arraylike_from_scalar (data , len (index ), dtype )
504
+
496
505
else :
497
506
subarr = _try_cast (data , dtype , copy , raise_cast_failure )
498
507
499
- # scalar like, GH
500
- if getattr (subarr , "ndim" , 0 ) == 0 :
501
- if isinstance (data , list ): # pragma: no cover
502
- subarr = np .array (data , dtype = object )
503
- elif index is not None :
504
- subarr = construct_1d_arraylike_from_scalar (data , len (index ), dtype )
505
-
506
- else :
507
- return subarr .item ()
508
-
509
- # the result that we want
510
- elif subarr .ndim == 1 :
511
- subarr = _maybe_repeat (subarr , index )
512
-
513
- elif subarr .ndim > 1 :
514
- if isinstance (data , np .ndarray ):
515
- raise ValueError ("Data must be 1-dimensional" )
516
- else :
517
- subarr = com .asarray_tuplesafe (data , dtype = dtype )
508
+ subarr = _sanitize_ndim (subarr , data , dtype , index )
518
509
519
510
if not (is_extension_array_dtype (subarr .dtype ) or is_extension_array_dtype (dtype )):
520
511
subarr = _sanitize_str_dtypes (subarr , data , dtype , copy )
@@ -528,6 +519,27 @@ def sanitize_array(
528
519
return subarr
529
520
530
521
522
+ def _sanitize_ndim (
523
+ result : ArrayLike , data , dtype : Optional [DtypeObj ], index : Optional [Index ]
524
+ ) -> ArrayLike :
525
+ """
526
+ Ensure we have a 1-dimensional result array.
527
+ """
528
+ if getattr (result , "ndim" , 0 ) == 0 :
529
+ raise ValueError ("result should be arraylike with ndim > 0" )
530
+
531
+ elif result .ndim == 1 :
532
+ # the result that we want
533
+ result = _maybe_repeat (result , index )
534
+
535
+ elif result .ndim > 1 :
536
+ if isinstance (data , np .ndarray ):
537
+ raise ValueError ("Data must be 1-dimensional" )
538
+ else :
539
+ result = com .asarray_tuplesafe (data , dtype = dtype )
540
+ return result
541
+
542
+
531
543
def _sanitize_str_dtypes (
532
544
result : np .ndarray , data , dtype : Optional [DtypeObj ], copy : bool
533
545
) -> np .ndarray :
@@ -565,7 +577,7 @@ def _try_cast(arr, dtype: Optional[DtypeObj], copy: bool, raise_cast_failure: bo
565
577
566
578
Parameters
567
579
----------
568
- arr : ndarray, scalar, list, tuple, iterator (catchall)
580
+ arr : ndarray, list, tuple, iterator (catchall)
569
581
Excludes: ExtensionArray, Series, Index.
570
582
dtype : np.dtype, ExtensionDtype or None
571
583
copy : bool
0 commit comments