Skip to content

Commit c5a3cf8

Browse files
jbrockmendelTLouf
authored andcommitted
REF: tighten types on maybe_infer_to_datetimelike (pandas-dev#41527)
1 parent 3022b62 commit c5a3cf8

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

pandas/core/construction.py

+7
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,13 @@ def _try_cast(
682682
subarr = construct_1d_object_array_from_listlike(arr)
683683
return subarr
684684

685+
if dtype is None and isinstance(arr, list):
686+
# filter out cases that we _dont_ want to go through maybe_cast_to_datetime
687+
varr = np.array(arr, copy=False)
688+
if varr.dtype != object or varr.size == 0:
689+
return varr
690+
arr = varr
691+
685692
try:
686693
# GH#15832: Check if we are requesting a numeric dtype and
687694
# that we can convert the data to the requested dtype.

pandas/core/dtypes/cast.py

+16-12
Original file line numberDiff line numberDiff line change
@@ -1480,7 +1480,9 @@ def maybe_castable(dtype: np.dtype) -> bool:
14801480
return dtype.name not in POSSIBLY_CAST_DTYPES
14811481

14821482

1483-
def maybe_infer_to_datetimelike(value: np.ndarray | list):
1483+
def maybe_infer_to_datetimelike(
1484+
value: np.ndarray,
1485+
) -> np.ndarray | DatetimeArray | TimedeltaArray:
14841486
"""
14851487
we might have a array (or single object) that is datetime like,
14861488
and no dtype is passed don't change the value unless we find a
@@ -1491,18 +1493,19 @@ def maybe_infer_to_datetimelike(value: np.ndarray | list):
14911493
14921494
Parameters
14931495
----------
1494-
value : np.ndarray or list
1496+
value : np.ndarray[object]
1497+
1498+
Returns
1499+
-------
1500+
np.ndarray, DatetimeArray, or TimedeltaArray
14951501
14961502
"""
1497-
if not isinstance(value, (np.ndarray, list)):
1503+
if not isinstance(value, np.ndarray) or value.dtype != object:
1504+
# Caller is responsible for passing only ndarray[object]
14981505
raise TypeError(type(value)) # pragma: no cover
14991506

15001507
v = np.array(value, copy=False)
15011508

1502-
# we only care about object dtypes
1503-
if not is_object_dtype(v.dtype):
1504-
return value
1505-
15061509
shape = v.shape
15071510
if v.ndim != 1:
15081511
v = v.ravel()
@@ -1580,6 +1583,8 @@ def maybe_cast_to_datetime(
15801583
"""
15811584
try to cast the array/value to a datetimelike dtype, converting float
15821585
nan to iNaT
1586+
1587+
We allow a list *only* when dtype is not None.
15831588
"""
15841589
from pandas.core.arrays.datetimes import sequence_to_datetimes
15851590
from pandas.core.arrays.timedeltas import sequence_to_td64ns
@@ -1671,11 +1676,10 @@ def maybe_cast_to_datetime(
16711676
value = maybe_infer_to_datetimelike(value)
16721677

16731678
elif isinstance(value, list):
1674-
# only do this if we have an array and the dtype of the array is not
1675-
# setup already we are not an integer/object, so don't bother with this
1676-
# conversion
1677-
1678-
value = maybe_infer_to_datetimelike(value)
1679+
# we only get here with dtype=None, which we do not allow
1680+
raise ValueError(
1681+
"maybe_cast_to_datetime allows a list *only* if dtype is not None"
1682+
)
16791683

16801684
return value
16811685

pandas/core/internals/construction.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,8 @@ def ndarray_to_mgr(
357357
if values.ndim == 2 and values.shape[0] != 1:
358358
# transpose and separate blocks
359359

360-
dvals_list = [maybe_infer_to_datetimelike(row) for row in values]
361-
dvals_list = [ensure_block_shape(dval, 2) for dval in dvals_list]
360+
dtlike_vals = [maybe_infer_to_datetimelike(row) for row in values]
361+
dvals_list = [ensure_block_shape(dval, 2) for dval in dtlike_vals]
362362

363363
# TODO: What about re-joining object columns?
364364
block_values = [

0 commit comments

Comments
 (0)