|
39 | 39 | construct_1d_object_array_from_listlike,
|
40 | 40 | maybe_cast_to_datetime,
|
41 | 41 | maybe_cast_to_integer_array,
|
42 |
| - maybe_castable, |
43 | 42 | maybe_convert_platform,
|
44 | 43 | maybe_upcast,
|
| 44 | + sanitize_to_nanoseconds, |
45 | 45 | )
|
46 | 46 | from pandas.core.dtypes.common import (
|
47 | 47 | is_datetime64_ns_dtype,
|
@@ -664,30 +664,45 @@ def _try_cast(
|
664 | 664 | # perf shortcut as this is the most common case
|
665 | 665 | if (
|
666 | 666 | isinstance(arr, np.ndarray)
|
667 |
| - and maybe_castable(arr.dtype) |
| 667 | + and arr.dtype != object |
668 | 668 | and not copy
|
669 | 669 | and dtype is None
|
670 | 670 | ):
|
671 |
| - return arr |
| 671 | + return sanitize_to_nanoseconds(arr) |
672 | 672 |
|
673 |
| - if isinstance(dtype, ExtensionDtype) and not isinstance(dtype, DatetimeTZDtype): |
| 673 | + if isinstance(dtype, ExtensionDtype): |
674 | 674 | # create an extension array from its dtype
|
675 | 675 | # DatetimeTZ case needs to go through maybe_cast_to_datetime but
|
676 | 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 | + |
677 | 688 | array_type = dtype.construct_array_type()._from_sequence
|
678 | 689 | subarr = array_type(arr, dtype=dtype, copy=copy)
|
679 | 690 | return subarr
|
680 | 691 |
|
681 |
| - if is_object_dtype(dtype) and not isinstance(arr, np.ndarray): |
682 |
| - subarr = construct_1d_object_array_from_listlike(arr) |
683 |
| - return subarr |
| 692 | + elif is_object_dtype(dtype): |
| 693 | + if not isinstance(arr, np.ndarray): |
| 694 | + subarr = construct_1d_object_array_from_listlike(arr) |
| 695 | + return subarr |
| 696 | + return ensure_wrapped_if_datetimelike(arr).astype(dtype, copy=copy) |
684 | 697 |
|
685 |
| - if dtype is None and isinstance(arr, list): |
| 698 | + elif dtype is None and isinstance(arr, list): |
686 | 699 | # filter out cases that we _dont_ want to go through maybe_cast_to_datetime
|
687 | 700 | varr = np.array(arr, copy=False)
|
688 | 701 | if varr.dtype != object or varr.size == 0:
|
689 | 702 | return varr
|
690 |
| - 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] |
691 | 706 |
|
692 | 707 | try:
|
693 | 708 | # GH#15832: Check if we are requesting a numeric dtype and
|
|
0 commit comments