From a0be12330ea7871492c81bbab62f21581aeaeff7 Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 28 May 2021 08:59:11 -0700 Subject: [PATCH 1/2] REF: simplify _try_cast --- pandas/core/construction.py | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/pandas/core/construction.py b/pandas/core/construction.py index 92f94f4424ee8..4bb5f06ad63b4 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -17,10 +17,7 @@ import numpy.ma as ma from pandas._libs import lib -from pandas._libs.tslibs import ( - IncompatibleFrequency, - OutOfBoundsDatetime, -) +from pandas._libs.tslibs import IncompatibleFrequency from pandas._typing import ( AnyArrayLike, ArrayLike, @@ -734,6 +731,9 @@ def _try_cast( return subarr return ensure_wrapped_if_datetimelike(arr).astype(dtype, copy=copy) + elif dtype.kind in ["m", "M"]: + return maybe_cast_to_datetime(arr, dtype) + try: # GH#15832: Check if we are requesting a numeric dtype and # that we can convert the data to the requested dtype. @@ -743,9 +743,7 @@ def _try_cast( maybe_cast_to_integer_array(arr, dtype) subarr = arr else: - subarr = maybe_cast_to_datetime(arr, dtype) - if dtype is not None and dtype.kind == "M": - return subarr + subarr = arr if not isinstance(subarr, ABCExtensionArray): # 4 tests fail if we move this to a try/except/else; see @@ -753,16 +751,8 @@ def _try_cast( # test_constructor_dict_cast2, test_loc_setitem_dtype subarr = construct_1d_ndarray_preserving_na(subarr, dtype, copy=copy) - except OutOfBoundsDatetime: - # in case of out of bound datetime64 -> always raise - raise - except (ValueError, TypeError) as err: - if dtype is not None and raise_cast_failure: - raise - elif "Cannot cast" in str(err) or "cannot be converted to timedelta64" in str( - err - ): - # via _disallow_mismatched_datetimelike + except (ValueError, TypeError): + if raise_cast_failure: raise else: subarr = np.array(arr, dtype=object, copy=copy) From e1760f021c78454184c6e149559451f076f9008c Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 28 May 2021 09:56:10 -0700 Subject: [PATCH 2/2] mypy fixup --- pandas/core/construction.py | 4 +--- pandas/core/dtypes/cast.py | 5 +++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/pandas/core/construction.py b/pandas/core/construction.py index 4bb5f06ad63b4..0267116cdfb99 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -716,9 +716,7 @@ def _try_cast( # while maybe_cast_to_datetime treats it as UTC # see test_maybe_promote_any_numpy_dtype_with_datetimetz - # error: Incompatible return value type (got "Union[ExtensionArray, - # ndarray, List[Any]]", expected "Union[ExtensionArray, ndarray]") - return maybe_cast_to_datetime(arr, dtype) # type: ignore[return-value] + return maybe_cast_to_datetime(arr, dtype) # TODO: copy? array_type = dtype.construct_array_type()._from_sequence diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 40883dd8f747b..d347f11d170c2 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -1579,7 +1579,7 @@ def try_timedelta(v: np.ndarray) -> np.ndarray: def maybe_cast_to_datetime( value: ExtensionArray | np.ndarray | list, dtype: DtypeObj | None -) -> ExtensionArray | np.ndarray | list: +) -> ExtensionArray | np.ndarray: """ try to cast the array/value to a datetimelike dtype, converting float nan to iNaT @@ -1689,7 +1689,8 @@ def maybe_cast_to_datetime( "maybe_cast_to_datetime allows a list *only* if dtype is not None" ) - return value + # at this point we have converted or raised in all cases where we had a list + return cast(ArrayLike, value) def sanitize_to_nanoseconds(values: np.ndarray, copy: bool = False) -> np.ndarray: