@@ -1422,7 +1422,7 @@ def maybe_infer_to_datetimelike(
1422
1422
v = np .array (v , copy = False )
1423
1423
1424
1424
# we only care about object dtypes
1425
- if not is_object_dtype (v ):
1425
+ if not is_object_dtype (v . dtype ):
1426
1426
return value
1427
1427
1428
1428
shape = v .shape
@@ -1499,7 +1499,9 @@ def try_timedelta(v: np.ndarray) -> np.ndarray:
1499
1499
return value
1500
1500
1501
1501
1502
- def maybe_cast_to_datetime (value , dtype : Optional [DtypeObj ]):
1502
+ def maybe_cast_to_datetime (
1503
+ value : Union [ExtensionArray , np .ndarray , list ], dtype : Optional [DtypeObj ]
1504
+ ) -> Union [ExtensionArray , np .ndarray , list ]:
1503
1505
"""
1504
1506
try to cast the array/value to a datetimelike dtype, converting float
1505
1507
nan to iNaT
@@ -1563,26 +1565,28 @@ def maybe_cast_to_datetime(value, dtype: Optional[DtypeObj]):
1563
1565
1564
1566
try :
1565
1567
if is_datetime64 :
1566
- value = to_datetime (value , errors = "raise" )
1568
+ dti = to_datetime (value , errors = "raise" )
1567
1569
# GH 25843: Remove tz information since the dtype
1568
1570
# didn't specify one
1569
- if value .tz is not None :
1570
- value = value .tz_localize (None )
1571
- value = value ._values
1571
+ if dti .tz is not None :
1572
+ dti = dti .tz_localize (None )
1573
+ value = dti ._values
1572
1574
elif is_datetime64tz :
1573
1575
# The string check can be removed once issue #13712
1574
1576
# is solved. String data that is passed with a
1575
1577
# datetime64tz is assumed to be naive which should
1576
1578
# be localized to the timezone.
1577
1579
is_dt_string = is_string_dtype (value .dtype )
1578
- value = to_datetime (value , errors = "raise" ).array
1579
- if is_dt_string :
1580
+ dta = to_datetime (value , errors = "raise" ).array
1581
+ if dta .tz is not None :
1582
+ value = dta .astype (dtype , copy = False )
1583
+ elif is_dt_string :
1580
1584
# Strings here are naive, so directly localize
1581
- value = value .tz_localize (dtype .tz )
1585
+ value = dta .tz_localize (dtype .tz )
1582
1586
else :
1583
1587
# Numeric values are UTC at this point,
1584
1588
# so localize and convert
1585
- value = value .tz_localize ("UTC" ).tz_convert (dtype .tz )
1589
+ value = dta .tz_localize ("UTC" ).tz_convert (dtype .tz )
1586
1590
elif is_timedelta64 :
1587
1591
value = to_timedelta (value , errors = "raise" )._values
1588
1592
except OutOfBoundsDatetime :
@@ -1595,6 +1599,8 @@ def maybe_cast_to_datetime(value, dtype: Optional[DtypeObj]):
1595
1599
getattr (value , "dtype" , None )
1596
1600
) and not is_datetime64_dtype (dtype ):
1597
1601
if is_object_dtype (dtype ):
1602
+ value = cast (np .ndarray , value )
1603
+
1598
1604
if value .dtype != DT64NS_DTYPE :
1599
1605
value = value .astype (DT64NS_DTYPE )
1600
1606
ints = np .asarray (value ).view ("i8" )
@@ -1603,25 +1609,20 @@ def maybe_cast_to_datetime(value, dtype: Optional[DtypeObj]):
1603
1609
# we have a non-castable dtype that was passed
1604
1610
raise TypeError (f"Cannot cast datetime64 to { dtype } " )
1605
1611
1606
- else :
1607
-
1608
- is_array = isinstance (value , np .ndarray )
1609
-
1610
- # catch a datetime/timedelta that is not of ns variety
1611
- # and no coercion specified
1612
- if is_array and value .dtype .kind in ["M" , "m" ]:
1612
+ elif isinstance (value , np .ndarray ):
1613
+ if value .dtype .kind in ["M" , "m" ]:
1614
+ # catch a datetime/timedelta that is not of ns variety
1615
+ # and no coercion specified
1613
1616
value = sanitize_to_nanoseconds (value )
1614
1617
1618
+ elif value .dtype == object :
1619
+ value = maybe_infer_to_datetimelike (value )
1620
+
1621
+ else :
1615
1622
# only do this if we have an array and the dtype of the array is not
1616
1623
# setup already we are not an integer/object, so don't bother with this
1617
1624
# conversion
1618
- elif not (
1619
- is_array
1620
- and not (
1621
- issubclass (value .dtype .type , np .integer ) or value .dtype == np .object_
1622
- )
1623
- ):
1624
- value = maybe_infer_to_datetimelike (value )
1625
+ value = maybe_infer_to_datetimelike (value )
1625
1626
1626
1627
return value
1627
1628
@@ -1835,7 +1836,9 @@ def construct_1d_ndarray_preserving_na(
1835
1836
return subarr
1836
1837
1837
1838
1838
- def maybe_cast_to_integer_array (arr , dtype : Dtype , copy : bool = False ):
1839
+ def maybe_cast_to_integer_array (
1840
+ arr : Union [list , np .ndarray ], dtype : np .dtype , copy : bool = False
1841
+ ):
1839
1842
"""
1840
1843
Takes any dtype and returns the casted version, raising for when data is
1841
1844
incompatible with integer/unsigned integer dtypes.
@@ -1844,9 +1847,9 @@ def maybe_cast_to_integer_array(arr, dtype: Dtype, copy: bool = False):
1844
1847
1845
1848
Parameters
1846
1849
----------
1847
- arr : array-like
1850
+ arr : np.ndarray or list
1848
1851
The array to cast.
1849
- dtype : str, np.dtype
1852
+ dtype : np.dtype
1850
1853
The integer dtype to cast the array to.
1851
1854
copy: bool, default False
1852
1855
Whether to make a copy of the array before returning.
@@ -1880,7 +1883,7 @@ def maybe_cast_to_integer_array(arr, dtype: Dtype, copy: bool = False):
1880
1883
assert is_integer_dtype (dtype )
1881
1884
1882
1885
try :
1883
- if not hasattr (arr , "astype" ):
1886
+ if not isinstance (arr , np . ndarray ):
1884
1887
casted = np .array (arr , dtype = dtype , copy = copy )
1885
1888
else :
1886
1889
casted = arr .astype (dtype , copy = copy )
0 commit comments