Skip to content

REF: tighter types in maybe_infer_to_datetimelike #40086

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 6 additions & 12 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@
from pandas.core.dtypes.generic import (
ABCDataFrame,
ABCExtensionArray,
ABCIndex,
ABCSeries,
)
from pandas.core.dtypes.inference import is_list_like
Expand Down Expand Up @@ -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,
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand Down