@@ -1232,22 +1232,20 @@ def maybe_infer_to_datetimelike(
1232
1232
if not len (v ):
1233
1233
return value
1234
1234
1235
- def try_datetime (v : np .ndarray ) -> ArrayLike :
1235
+ def try_datetime (v : np .ndarray ) -> np . ndarray | DatetimeArray :
1236
1236
# Coerce to datetime64, datetime64tz, or in corner cases
1237
1237
# object[datetimes]
1238
1238
from pandas .core .arrays .datetimes import sequence_to_datetimes
1239
1239
1240
1240
try :
1241
- # GH#19671 we pass require_iso8601 to be relatively strict
1242
- # when parsing strings.
1243
- dta = sequence_to_datetimes (v , require_iso8601 = True )
1244
- except (ValueError , TypeError ):
1245
- # e.g. <class 'numpy.timedelta64'> is not convertible to datetime
1246
- return v .reshape (shape )
1247
- else :
1241
+ dta = sequence_to_datetimes (v )
1242
+ except (ValueError , OutOfBoundsDatetime ):
1243
+ # ValueError for e.g. mixed tzs
1248
1244
# GH#19761 we may have mixed timezones, in which cast 'dta' is
1249
1245
# an ndarray[object]. Only 1 test
1250
1246
# relies on this behavior, see GH#40111
1247
+ return v .reshape (shape )
1248
+ else :
1251
1249
return dta .reshape (shape )
1252
1250
1253
1251
def try_timedelta (v : np .ndarray ) -> np .ndarray :
@@ -1259,12 +1257,14 @@ def try_timedelta(v: np.ndarray) -> np.ndarray:
1259
1257
# `np.asarray(to_timedelta(v))`, but using a lower-level API that
1260
1258
# does not require a circular import.
1261
1259
td_values = array_to_timedelta64 (v ).view ("m8[ns]" )
1262
- except ( ValueError , OverflowError ) :
1260
+ except OutOfBoundsTimedelta :
1263
1261
return v .reshape (shape )
1264
1262
else :
1265
1263
return td_values .reshape (shape )
1266
1264
1267
- # TODO: can we just do lib.maybe_convert_objects for this entire function?
1265
+ # TODO: this is _almost_ equivalent to lib.maybe_convert_objects,
1266
+ # the main differences are described in GH#49340 and GH#49341
1267
+ # and maybe_convert_objects doesn't catch OutOfBoundsDatetime
1268
1268
inferred_type = lib .infer_datetimelike_array (ensure_object (v ))
1269
1269
1270
1270
if inferred_type in ["period" , "interval" ]:
@@ -1276,31 +1276,21 @@ def try_timedelta(v: np.ndarray) -> np.ndarray:
1276
1276
)
1277
1277
1278
1278
if inferred_type == "datetime" :
1279
- # error: Incompatible types in assignment (expression has type "ExtensionArray",
1280
- # variable has type "Union[ndarray, List[Any]]")
1279
+ # Incompatible types in assignment (expression has type
1280
+ # "Union[ndarray[Any, Any], DatetimeArray]", variable has type
1281
+ # "ndarray[Any, Any]")
1281
1282
value = try_datetime (v ) # type: ignore[assignment]
1282
1283
elif inferred_type == "timedelta" :
1283
1284
value = try_timedelta (v )
1284
1285
elif inferred_type == "nat" :
1286
+ # if all NaT, return as datetime
1285
1287
# only reached if we have at least 1 NaT and the rest (NaT or None or np.nan)
1286
1288
1287
- # if all NaT, return as datetime
1288
- if isna (v ).all ():
1289
- # error: Incompatible types in assignment (expression has type
1290
- # "ExtensionArray", variable has type "Union[ndarray, List[Any]]")
1291
- value = try_datetime (v ) # type: ignore[assignment]
1292
- else :
1293
- # We have at least a NaT and a string
1294
- # try timedelta first to avoid spurious datetime conversions
1295
- # e.g. '00:00:01' is a timedelta but technically is also a datetime
1296
- value = try_timedelta (v )
1297
- if lib .infer_dtype (value , skipna = False ) in ["mixed" ]:
1298
- # cannot skip missing values, as NaT implies that the string
1299
- # is actually a datetime
1300
-
1301
- # error: Incompatible types in assignment (expression has type
1302
- # "ExtensionArray", variable has type "Union[ndarray, List[Any]]")
1303
- value = try_datetime (v ) # type: ignore[assignment]
1289
+ # Incompatible types in assignment (expression has type
1290
+ # "Union[ndarray[Any, Any], DatetimeArray]", variable has type
1291
+ # "ndarray[Any, Any]")
1292
+ value = try_datetime (v ) # type: ignore[assignment]
1293
+ assert value .dtype == "M8[ns]"
1304
1294
1305
1295
return value
1306
1296
0 commit comments