65
65
is_complex ,
66
66
is_complex_dtype ,
67
67
is_datetime64_dtype ,
68
- is_datetime64tz_dtype ,
69
68
is_dtype_equal ,
70
69
is_extension_array_dtype ,
71
70
is_float ,
@@ -1314,13 +1313,15 @@ def try_timedelta(v: np.ndarray) -> np.ndarray:
1314
1313
1315
1314
1316
1315
def maybe_cast_to_datetime (
1317
- value : ExtensionArray | np .ndarray | list , dtype : DtypeObj | None
1316
+ value : ExtensionArray | np .ndarray | list , dtype : np . dtype | None
1318
1317
) -> ExtensionArray | np .ndarray :
1319
1318
"""
1320
1319
try to cast the array/value to a datetimelike dtype, converting float
1321
1320
nan to iNaT
1322
1321
1323
1322
We allow a list *only* when dtype is not None.
1323
+
1324
+ Caller is responsible for handling ExtensionDtype cases.
1324
1325
"""
1325
1326
from pandas .core .arrays .datetimes import sequence_to_datetimes
1326
1327
from pandas .core .arrays .timedeltas import TimedeltaArray
@@ -1332,18 +1333,22 @@ def maybe_cast_to_datetime(
1332
1333
# TODO: _from_sequence would raise ValueError in cases where
1333
1334
# _ensure_nanosecond_dtype raises TypeError
1334
1335
dtype = cast (np .dtype , dtype )
1335
- dtype = _ensure_nanosecond_dtype (dtype )
1336
+ # Incompatible types in assignment (expression has type "Union[dtype[Any],
1337
+ # ExtensionDtype]", variable has type "Optional[dtype[Any]]")
1338
+ dtype = _ensure_nanosecond_dtype (dtype ) # type: ignore[assignment]
1336
1339
res = TimedeltaArray ._from_sequence (value , dtype = dtype )
1337
1340
return res
1338
1341
1339
1342
if dtype is not None :
1340
1343
is_datetime64 = is_datetime64_dtype (dtype )
1341
- is_datetime64tz = is_datetime64tz_dtype (dtype )
1342
1344
1343
1345
vdtype = getattr (value , "dtype" , None )
1344
1346
1345
- if is_datetime64 or is_datetime64tz :
1346
- dtype = _ensure_nanosecond_dtype (dtype )
1347
+ if is_datetime64 :
1348
+ # Incompatible types in assignment (expression has type
1349
+ # "Union[dtype[Any], ExtensionDtype]", variable has type
1350
+ # "Optional[dtype[Any]]")
1351
+ dtype = _ensure_nanosecond_dtype (dtype ) # type: ignore[assignment]
1347
1352
1348
1353
value = np .array (value , copy = False )
1349
1354
@@ -1352,59 +1357,22 @@ def maybe_cast_to_datetime(
1352
1357
_disallow_mismatched_datetimelike (value , dtype )
1353
1358
1354
1359
try :
1355
- if is_datetime64 :
1356
- dta = sequence_to_datetimes (value )
1357
- # GH 25843: Remove tz information since the dtype
1358
- # didn't specify one
1359
-
1360
- if dta .tz is not None :
1361
- raise ValueError (
1362
- "Cannot convert timezone-aware data to "
1363
- "timezone-naive dtype. Use "
1364
- "pd.Series(values).dt.tz_localize(None) instead."
1365
- )
1366
-
1367
- # TODO(2.0): Do this astype in sequence_to_datetimes to
1368
- # avoid potential extra copy?
1369
- dta = dta .astype (dtype , copy = False )
1370
- value = dta
1371
- elif is_datetime64tz :
1372
- dtype = cast (DatetimeTZDtype , dtype )
1373
- # The string check can be removed once issue #13712
1374
- # is solved. String data that is passed with a
1375
- # datetime64tz is assumed to be naive which should
1376
- # be localized to the timezone.
1377
- is_dt_string = is_string_dtype (value .dtype )
1378
- dta = sequence_to_datetimes (value )
1379
- if dta .tz is not None :
1380
- value = dta .astype (dtype , copy = False )
1381
- elif is_dt_string :
1382
- # Strings here are naive, so directly localize
1383
- # equiv: dta.astype(dtype) # though deprecated
1384
-
1385
- value = dta .tz_localize (dtype .tz )
1386
- else :
1387
- # Numeric values are UTC at this point,
1388
- # so localize and convert
1389
- # equiv: Series(dta).astype(dtype) # though deprecated
1390
- if getattr (vdtype , "kind" , None ) == "M" :
1391
- # GH#24559, GH#33401 deprecate behavior inconsistent
1392
- # with DatetimeArray/DatetimeIndex
1393
- warnings .warn (
1394
- "In a future version, constructing a Series "
1395
- "from datetime64[ns] data and a "
1396
- "DatetimeTZDtype will interpret the data "
1397
- "as wall-times instead of "
1398
- "UTC times, matching the behavior of "
1399
- "DatetimeIndex. To treat the data as UTC "
1400
- "times, use pd.Series(data).dt"
1401
- ".tz_localize('UTC').tz_convert(dtype.tz) "
1402
- "or pd.Series(data.view('int64'), dtype=dtype)" ,
1403
- FutureWarning ,
1404
- stacklevel = find_stack_level (),
1405
- )
1406
-
1407
- value = dta .tz_localize ("UTC" ).tz_convert (dtype .tz )
1360
+ dta = sequence_to_datetimes (value )
1361
+ # GH 25843: Remove tz information since the dtype
1362
+ # didn't specify one
1363
+
1364
+ if dta .tz is not None :
1365
+ raise ValueError (
1366
+ "Cannot convert timezone-aware data to "
1367
+ "timezone-naive dtype. Use "
1368
+ "pd.Series(values).dt.tz_localize(None) instead."
1369
+ )
1370
+
1371
+ # TODO(2.0): Do this astype in sequence_to_datetimes to
1372
+ # avoid potential extra copy?
1373
+ dta = dta .astype (dtype , copy = False )
1374
+ value = dta
1375
+
1408
1376
except OutOfBoundsDatetime :
1409
1377
raise
1410
1378
except ParserError :
0 commit comments