@@ -468,32 +468,27 @@ def sanitize_array(data, index, dtype=None, copy=False, raise_cast_failure=False
468
468
else :
469
469
subarr = com .asarray_tuplesafe (data , dtype = dtype )
470
470
471
- # This is to prevent mixed-type Series getting all casted to
472
- # NumPy string type, e.g. NaN --> '-1#IND'.
473
- if not (
474
- is_extension_array_dtype (subarr .dtype ) or is_extension_array_dtype (dtype )
475
- ) and issubclass (subarr .dtype .type , str ):
476
- # GH#16605
477
- # If not empty convert the data to dtype
478
- # GH#19853: If data is a scalar, subarr has already the result
479
- if not lib .is_scalar (data ):
480
- if not np .all (isna (data )):
481
- data = np .array (data , dtype = dtype , copy = False )
482
- subarr = np .array (data , dtype = object , copy = copy )
483
-
484
- if (
485
- not (is_extension_array_dtype (subarr .dtype ) or is_extension_array_dtype (dtype ))
486
- and is_object_dtype (subarr .dtype )
487
- and not is_object_dtype (dtype )
488
- ):
489
- inferred = lib .infer_dtype (subarr , skipna = False )
490
- if inferred == "period" :
491
- from pandas .core .arrays import period_array
471
+ if not (is_extension_array_dtype (subarr .dtype ) or is_extension_array_dtype (dtype )):
472
+ # This is to prevent mixed-type Series getting all casted to
473
+ # NumPy string type, e.g. NaN --> '-1#IND'.
474
+ if issubclass (subarr .dtype .type , str ):
475
+ # GH#16605
476
+ # If not empty convert the data to dtype
477
+ # GH#19853: If data is a scalar, subarr has already the result
478
+ if not lib .is_scalar (data ):
479
+ if not np .all (isna (data )):
480
+ data = np .array (data , dtype = dtype , copy = False )
481
+ subarr = np .array (data , dtype = object , copy = copy )
492
482
493
- try :
494
- subarr = period_array (subarr )
495
- except IncompatibleFrequency :
496
- pass
483
+ if is_object_dtype (subarr .dtype ) and not is_object_dtype (dtype ):
484
+ inferred = lib .infer_dtype (subarr , skipna = False )
485
+ if inferred == "period" :
486
+ from pandas .core .arrays import period_array
487
+
488
+ try :
489
+ subarr = period_array (subarr )
490
+ except IncompatibleFrequency :
491
+ pass
497
492
498
493
return subarr
499
494
0 commit comments