39
39
maybe_cast_to_datetime ,
40
40
maybe_cast_to_integer_array ,
41
41
maybe_convert_platform ,
42
+ maybe_infer_to_datetimelike ,
42
43
maybe_upcast ,
43
44
sanitize_to_nanoseconds ,
44
45
)
@@ -546,11 +547,12 @@ def sanitize_array(
546
547
if dtype is not None or len (data ) == 0 :
547
548
subarr = _try_cast (data , dtype , copy , raise_cast_failure )
548
549
else :
550
+ # TODO: copy?
549
551
subarr = maybe_convert_platform (data )
550
- # error: Incompatible types in assignment (expression has type
551
- # "Union[ExtensionArray, ndarray, List[Any]]", variable has type
552
- # " ExtensionArray")
553
- subarr = maybe_cast_to_datetime (subarr , dtype ) # type: ignore[assignment ]
552
+ if subarr . dtype == object :
553
+ # Argument 1 to "maybe_infer_to_datetimelike" has incompatible
554
+ # type "Union[ ExtensionArray, ndarray]"; expected "ndarray"
555
+ subarr = maybe_infer_to_datetimelike (subarr ) # type: ignore[arg-type ]
554
556
555
557
subarr = _sanitize_ndim (subarr , data , dtype , index )
556
558
@@ -677,22 +679,29 @@ def _try_cast(
677
679
"""
678
680
is_ndarray = isinstance (arr , np .ndarray )
679
681
680
- # perf shortcut as this is the most common case
681
- # Item "List[Any]" of "Union[List[Any], ndarray]" has no attribute "dtype"
682
- if (
683
- is_ndarray
684
- and arr .dtype != object # type: ignore[union-attr]
685
- and not copy
686
- and dtype is None
687
- ):
688
- # Argument 1 to "sanitize_to_nanoseconds" has incompatible type
689
- # "Union[List[Any], ndarray]"; expected "ndarray"
690
- return sanitize_to_nanoseconds ( arr ) # type: ignore[arg-type]
682
+ if dtype is None :
683
+ # perf shortcut as this is the most common case
684
+ if is_ndarray :
685
+ arr = cast ( np . ndarray , arr )
686
+ if arr .dtype != object :
687
+ return sanitize_to_nanoseconds ( arr , copy = copy )
688
+
689
+ out = maybe_infer_to_datetimelike ( arr )
690
+ if out is arr and copy :
691
+ out = out . copy ()
692
+ return out
691
693
692
- if isinstance (dtype , ExtensionDtype ):
694
+ else :
695
+ # i.e. list
696
+ varr = np .array (arr , copy = False )
697
+ # filter out cases that we _dont_ want to go through
698
+ # maybe_infer_to_datetimelike
699
+ if varr .dtype != object or varr .size == 0 :
700
+ return varr
701
+ return maybe_infer_to_datetimelike (varr )
702
+
703
+ elif isinstance (dtype , ExtensionDtype ):
693
704
# create an extension array from its dtype
694
- # DatetimeTZ case needs to go through maybe_cast_to_datetime but
695
- # SparseDtype does not
696
705
if isinstance (dtype , DatetimeTZDtype ):
697
706
# We can't go through _from_sequence because it handles dt64naive
698
707
# data differently; _from_sequence treats naive as wall times,
@@ -714,22 +723,12 @@ def _try_cast(
714
723
return subarr
715
724
return ensure_wrapped_if_datetimelike (arr ).astype (dtype , copy = copy )
716
725
717
- elif dtype is None and not is_ndarray :
718
- # filter out cases that we _dont_ want to go through maybe_cast_to_datetime
719
- varr = np .array (arr , copy = False )
720
- if varr .dtype != object or varr .size == 0 :
721
- return varr
722
- # error: Incompatible return value type (got "Union[ExtensionArray,
723
- # ndarray, List[Any]]", expected "Union[ExtensionArray, ndarray]")
724
- return maybe_cast_to_datetime (varr , None ) # type: ignore[return-value]
725
-
726
726
try :
727
727
# GH#15832: Check if we are requesting a numeric dtype and
728
728
# that we can convert the data to the requested dtype.
729
729
if is_integer_dtype (dtype ):
730
730
# this will raise if we have e.g. floats
731
731
732
- dtype = cast (np .dtype , dtype )
733
732
maybe_cast_to_integer_array (arr , dtype )
734
733
subarr = arr
735
734
else :
@@ -738,7 +737,11 @@ def _try_cast(
738
737
return subarr
739
738
740
739
if not isinstance (subarr , ABCExtensionArray ):
740
+ # 4 tests fail if we move this to a try/except/else; see
741
+ # test_constructor_compound_dtypes, test_constructor_cast_failure
742
+ # test_constructor_dict_cast2, test_loc_setitem_dtype
741
743
subarr = construct_1d_ndarray_preserving_na (subarr , dtype , copy = copy )
744
+
742
745
except OutOfBoundsDatetime :
743
746
# in case of out of bound datetime64 -> always raise
744
747
raise
0 commit comments