@@ -1480,7 +1480,9 @@ def maybe_castable(dtype: np.dtype) -> bool:
1480
1480
return dtype .name not in POSSIBLY_CAST_DTYPES
1481
1481
1482
1482
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 :
1484
1486
"""
1485
1487
we might have a array (or single object) that is datetime like,
1486
1488
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):
1491
1493
1492
1494
Parameters
1493
1495
----------
1494
- value : np.ndarray or list
1496
+ value : np.ndarray[object]
1497
+
1498
+ Returns
1499
+ -------
1500
+ np.ndarray, DatetimeArray, or TimedeltaArray
1495
1501
1496
1502
"""
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]
1498
1505
raise TypeError (type (value )) # pragma: no cover
1499
1506
1500
1507
v = np .array (value , copy = False )
1501
1508
1502
- # we only care about object dtypes
1503
- if not is_object_dtype (v .dtype ):
1504
- return value
1505
-
1506
1509
shape = v .shape
1507
1510
if v .ndim != 1 :
1508
1511
v = v .ravel ()
@@ -1580,6 +1583,8 @@ def maybe_cast_to_datetime(
1580
1583
"""
1581
1584
try to cast the array/value to a datetimelike dtype, converting float
1582
1585
nan to iNaT
1586
+
1587
+ We allow a list *only* when dtype is not None.
1583
1588
"""
1584
1589
from pandas .core .arrays .datetimes import sequence_to_datetimes
1585
1590
from pandas .core .arrays .timedeltas import sequence_to_td64ns
@@ -1671,11 +1676,10 @@ def maybe_cast_to_datetime(
1671
1676
value = maybe_infer_to_datetimelike (value )
1672
1677
1673
1678
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
+ )
1679
1683
1680
1684
return value
1681
1685
0 commit comments