|
38 | 38 | construct_1d_object_array_from_listlike,
|
39 | 39 | maybe_cast_to_datetime,
|
40 | 40 | maybe_cast_to_integer_array,
|
41 |
| - maybe_castable, |
42 | 41 | maybe_convert_platform,
|
43 | 42 | maybe_upcast,
|
| 43 | + sanitize_to_nanoseconds, |
44 | 44 | )
|
45 | 45 | from pandas.core.dtypes.common import (
|
46 | 46 | is_datetime64_ns_dtype,
|
@@ -656,33 +656,53 @@ def _try_cast(
|
656 | 656 | -------
|
657 | 657 | np.ndarray or ExtensionArray
|
658 | 658 | """
|
| 659 | + is_ndarray = isinstance(arr, np.ndarray) |
| 660 | + |
659 | 661 | # perf shortcut as this is the most common case
|
| 662 | + # Item "List[Any]" of "Union[List[Any], ndarray]" has no attribute "dtype" |
660 | 663 | if (
|
661 |
| - isinstance(arr, np.ndarray) |
662 |
| - and maybe_castable(arr.dtype) |
| 664 | + is_ndarray |
| 665 | + and arr.dtype != object # type: ignore[union-attr] |
663 | 666 | and not copy
|
664 | 667 | and dtype is None
|
665 | 668 | ):
|
666 |
| - return arr |
| 669 | + # Argument 1 to "sanitize_to_nanoseconds" has incompatible type |
| 670 | + # "Union[List[Any], ndarray]"; expected "ndarray" |
| 671 | + return sanitize_to_nanoseconds(arr) # type: ignore[arg-type] |
667 | 672 |
|
668 |
| - if isinstance(dtype, ExtensionDtype) and not isinstance(dtype, DatetimeTZDtype): |
| 673 | + if isinstance(dtype, ExtensionDtype): |
669 | 674 | # create an extension array from its dtype
|
670 | 675 | # DatetimeTZ case needs to go through maybe_cast_to_datetime but
|
671 | 676 | # SparseDtype does not
|
| 677 | + if isinstance(dtype, DatetimeTZDtype): |
| 678 | + # We can't go through _from_sequence because it handles dt64naive |
| 679 | + # data differently; _from_sequence treats naive as wall times, |
| 680 | + # while maybe_cast_to_datetime treats it as UTC |
| 681 | + # see test_maybe_promote_any_numpy_dtype_with_datetimetz |
| 682 | + |
| 683 | + # error: Incompatible return value type (got "Union[ExtensionArray, |
| 684 | + # ndarray, List[Any]]", expected "Union[ExtensionArray, ndarray]") |
| 685 | + return maybe_cast_to_datetime(arr, dtype) # type: ignore[return-value] |
| 686 | + # TODO: copy? |
| 687 | + |
672 | 688 | array_type = dtype.construct_array_type()._from_sequence
|
673 | 689 | subarr = array_type(arr, dtype=dtype, copy=copy)
|
674 | 690 | return subarr
|
675 | 691 |
|
676 |
| - if is_object_dtype(dtype) and not isinstance(arr, np.ndarray): |
677 |
| - subarr = construct_1d_object_array_from_listlike(arr) |
678 |
| - return subarr |
| 692 | + elif is_object_dtype(dtype): |
| 693 | + if not is_ndarray: |
| 694 | + subarr = construct_1d_object_array_from_listlike(arr) |
| 695 | + return subarr |
| 696 | + return ensure_wrapped_if_datetimelike(arr).astype(dtype, copy=copy) |
679 | 697 |
|
680 |
| - if dtype is None and isinstance(arr, list): |
| 698 | + elif dtype is None and not is_ndarray: |
681 | 699 | # filter out cases that we _dont_ want to go through maybe_cast_to_datetime
|
682 | 700 | varr = np.array(arr, copy=False)
|
683 | 701 | if varr.dtype != object or varr.size == 0:
|
684 | 702 | return varr
|
685 |
| - arr = varr |
| 703 | + # error: Incompatible return value type (got "Union[ExtensionArray, |
| 704 | + # ndarray, List[Any]]", expected "Union[ExtensionArray, ndarray]") |
| 705 | + return maybe_cast_to_datetime(varr, None) # type: ignore[return-value] |
686 | 706 |
|
687 | 707 | try:
|
688 | 708 | # GH#15832: Check if we are requesting a numeric dtype and
|
|
0 commit comments