From 19f3cc4d39a7d3f35d8a8aced2a11be988ba846b Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 26 Feb 2021 20:27:08 -0800 Subject: [PATCH 1/2] annotate --- pandas/core/arrays/timedeltas.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pandas/core/arrays/timedeltas.py b/pandas/core/arrays/timedeltas.py index f7af1bb3da86b..27361b83ee88f 100644 --- a/pandas/core/arrays/timedeltas.py +++ b/pandas/core/arrays/timedeltas.py @@ -4,6 +4,7 @@ from typing import ( List, Optional, + Tuple, Union, ) @@ -907,7 +908,9 @@ def f(x): # Constructor Helpers -def sequence_to_td64ns(data, copy=False, unit=None, errors="raise"): +def sequence_to_td64ns( + data, copy=False, unit=None, errors="raise" +) -> Tuple[np.ndarray, Optional[Tick]]: """ Parameters ---------- From 11cfafce63c6ac34bffc610188f23d5f21327d05 Mon Sep 17 00:00:00 2001 From: Brock Date: Sat, 27 Feb 2021 18:09:37 -0800 Subject: [PATCH 2/2] REF: simplify maybe_coerce_to_datetimelike --- pandas/core/dtypes/cast.py | 56 +++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index a93bf0c9211e1..8afd5ce179e50 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -1416,31 +1416,43 @@ def maybe_infer_to_datetimelike(value: Union[np.ndarray, List]): return value def try_datetime(v: np.ndarray) -> ArrayLike: - # safe coerce to datetime64 - try: - # GH19671 - # tznaive only - v = tslib.array_to_datetime(v, require_iso8601=True, errors="raise")[0] - except ValueError: + # Coerce to datetime64, datetime64tz, or in corner cases + # object[datetimes] + from pandas.core.arrays.datetimes import ( + DatetimeArray, + objects_to_datetime64ns, + tz_to_dtype, + ) + try: + # GH#19671 we pass require_iso8601 to be relatively strict + # when parsing strings. + vals, tz = objects_to_datetime64ns( + v, + require_iso8601=True, + dayfirst=False, + yearfirst=False, + allow_object=True, + ) + except (ValueError, TypeError): + # e.g. is not convertible to datetime + return v.reshape(shape) + else: # we might have a sequence of the same-datetimes with tz's # if so coerce to a DatetimeIndex; if they are not the same, - # then these stay as object dtype, xref GH19671 - from pandas import DatetimeIndex - - try: - - values, tz = conversion.datetime_to_datetime64(v) - except (ValueError, TypeError): - pass - else: - dti = DatetimeIndex(values).tz_localize("UTC").tz_convert(tz=tz) - return dti._data - except TypeError: - # e.g. is not convertible to datetime - pass - - return v.reshape(shape) + # then these stay as object dtype, xref GH#19671 + + if vals.dtype == object: + # This is reachable bc allow_object=True, means we cast things + # to mixed-tz datetime objects (mostly). Only 1 test + # relies on this behavior, see GH#40111 + return vals.reshape(shape) + + dta = DatetimeArray._simple_new(vals.view("M8[ns]"), dtype=tz_to_dtype(tz)) + if dta.tz is None: + # TODO(EA2D): conditional reshape kludge unnecessary with 2D EAs + return dta._ndarray.reshape(shape) + return dta def try_timedelta(v: np.ndarray) -> np.ndarray: # safe coerce to timedelta64