Skip to content

Commit 739f550

Browse files
authored
REF: tighter types in maybe_infer_to_datetimelike (#40086)
1 parent bdcefdd commit 739f550

File tree

3 files changed

+11
-13
lines changed

3 files changed

+11
-13
lines changed

pandas/core/arrays/categorical.py

+4
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,10 @@ def __init__(
398398
elif not isinstance(values, (ABCIndex, ABCSeries, ExtensionArray)):
399399
# sanitize_array coerces np.nan to a string under certain versions
400400
# of numpy
401+
if not isinstance(values, (np.ndarray, list)):
402+
# convert e.g. range, tuple to allow for stronger typing
403+
# of maybe_infer_to_datetimelike
404+
values = list(values)
401405
values = maybe_infer_to_datetimelike(values)
402406
if isinstance(values, np.ndarray):
403407
values = sanitize_to_nanoseconds(values)

pandas/core/construction.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ def _try_cast(
670670
else:
671671
subarr = maybe_cast_to_datetime(arr, dtype)
672672

673-
if not isinstance(subarr, (ABCExtensionArray, ABCIndex)):
673+
if not isinstance(subarr, ABCExtensionArray):
674674
subarr = construct_1d_ndarray_preserving_na(subarr, dtype, copy=copy)
675675
except OutOfBoundsDatetime:
676676
# in case of out of bound datetime64 -> always raise

pandas/core/dtypes/cast.py

+6-12
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@
9696
from pandas.core.dtypes.generic import (
9797
ABCDataFrame,
9898
ABCExtensionArray,
99-
ABCIndex,
10099
ABCSeries,
101100
)
102101
from pandas.core.dtypes.inference import is_list_like
@@ -1389,7 +1388,7 @@ def maybe_castable(dtype: np.dtype) -> bool:
13891388

13901389

13911390
def maybe_infer_to_datetimelike(
1392-
value: Union[ArrayLike, Scalar], convert_dates: bool = False
1391+
value: Union[np.ndarray, List], convert_dates: bool = False
13931392
):
13941393
"""
13951394
we might have a array (or single object) that is datetime like,
@@ -1401,21 +1400,16 @@ def maybe_infer_to_datetimelike(
14011400
14021401
Parameters
14031402
----------
1404-
value : np.array / Series / Index / list-like
1403+
value : np.ndarray or list
14051404
convert_dates : bool, default False
14061405
if True try really hard to convert dates (such as datetime.date), other
14071406
leave inferred dtype 'date' alone
14081407
14091408
"""
1410-
if isinstance(value, (ABCIndex, ABCExtensionArray)):
1411-
if not is_object_dtype(value.dtype):
1412-
raise ValueError("array-like value must be object-dtype")
1409+
if not isinstance(value, (np.ndarray, list)):
1410+
raise TypeError(type(value))
14131411

1414-
v = value
1415-
1416-
if not is_list_like(v):
1417-
v = [v]
1418-
v = np.array(v, copy=False)
1412+
v = np.array(value, copy=False)
14191413

14201414
# we only care about object dtypes
14211415
if not is_object_dtype(v.dtype):
@@ -1616,7 +1610,7 @@ def maybe_cast_to_datetime(
16161610
elif value.dtype == object:
16171611
value = maybe_infer_to_datetimelike(value)
16181612

1619-
else:
1613+
elif not isinstance(value, ABCExtensionArray):
16201614
# only do this if we have an array and the dtype of the array is not
16211615
# setup already we are not an integer/object, so don't bother with this
16221616
# conversion

0 commit comments

Comments
 (0)