From 39af6ef164a14fab52af5ec9c3cc6bfbe30f548a Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 26 Feb 2021 09:30:56 -0800 Subject: [PATCH] REF: tighter types in maybe_infer_to_datetimelike --- pandas/core/arrays/categorical.py | 4 ++++ pandas/core/construction.py | 2 +- pandas/core/dtypes/cast.py | 18 ++++++------------ 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index 29a172dcdd2c7..bdc64d5f14a27 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -398,6 +398,10 @@ def __init__( elif not isinstance(values, (ABCIndex, ABCSeries, ExtensionArray)): # sanitize_array coerces np.nan to a string under certain versions # of numpy + if not isinstance(values, (np.ndarray, list)): + # convert e.g. range, tuple to allow for stronger typing + # of maybe_infer_to_datetimelike + values = list(values) values = maybe_infer_to_datetimelike(values) if isinstance(values, np.ndarray): values = sanitize_to_nanoseconds(values) diff --git a/pandas/core/construction.py b/pandas/core/construction.py index 0c0084f2492d3..f5f49e0e5fc20 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -670,7 +670,7 @@ def _try_cast( else: subarr = maybe_cast_to_datetime(arr, dtype) - if not isinstance(subarr, (ABCExtensionArray, ABCIndex)): + if not isinstance(subarr, ABCExtensionArray): subarr = construct_1d_ndarray_preserving_na(subarr, dtype, copy=copy) except OutOfBoundsDatetime: # in case of out of bound datetime64 -> always raise diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index b30dbe32eec4b..8d25fc2e4aded 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -96,7 +96,6 @@ from pandas.core.dtypes.generic import ( ABCDataFrame, ABCExtensionArray, - ABCIndex, ABCSeries, ) from pandas.core.dtypes.inference import is_list_like @@ -1389,7 +1388,7 @@ def maybe_castable(dtype: np.dtype) -> bool: def maybe_infer_to_datetimelike( - value: Union[ArrayLike, Scalar], convert_dates: bool = False + value: Union[np.ndarray, List], convert_dates: bool = False ): """ we might have a array (or single object) that is datetime like, @@ -1401,21 +1400,16 @@ def maybe_infer_to_datetimelike( Parameters ---------- - value : np.array / Series / Index / list-like + value : np.ndarray or list convert_dates : bool, default False if True try really hard to convert dates (such as datetime.date), other leave inferred dtype 'date' alone """ - if isinstance(value, (ABCIndex, ABCExtensionArray)): - if not is_object_dtype(value.dtype): - raise ValueError("array-like value must be object-dtype") + if not isinstance(value, (np.ndarray, list)): + raise TypeError(type(value)) - v = value - - if not is_list_like(v): - v = [v] - v = np.array(v, copy=False) + v = np.array(value, copy=False) # we only care about object dtypes if not is_object_dtype(v.dtype): @@ -1616,7 +1610,7 @@ def maybe_cast_to_datetime( elif value.dtype == object: value = maybe_infer_to_datetimelike(value) - else: + elif not isinstance(value, ABCExtensionArray): # only do this if we have an array and the dtype of the array is not # setup already we are not an integer/object, so don't bother with this # conversion