Skip to content

Commit 503335d

Browse files
committed
Pull ExtensionArray check one level up
1 parent 38e37be commit 503335d

File tree

1 file changed

+20
-25
lines changed

1 file changed

+20
-25
lines changed

pandas/core/construction.py

+20-25
Original file line numberDiff line numberDiff line change
@@ -468,32 +468,27 @@ def sanitize_array(data, index, dtype=None, copy=False, raise_cast_failure=False
468468
else:
469469
subarr = com.asarray_tuplesafe(data, dtype=dtype)
470470

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)
492482

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
497492

498493
return subarr
499494

0 commit comments

Comments
 (0)