6565 is_complex ,
6666 is_complex_dtype ,
6767 is_datetime64_dtype ,
68- is_datetime64tz_dtype ,
6968 is_dtype_equal ,
7069 is_extension_array_dtype ,
7170 is_float ,
@@ -1314,13 +1313,15 @@ def try_timedelta(v: np.ndarray) -> np.ndarray:
13141313
13151314
13161315def maybe_cast_to_datetime (
1317- value : ExtensionArray | np .ndarray | list , dtype : DtypeObj | None
1316+ value : ExtensionArray | np .ndarray | list , dtype : np . dtype | None
13181317) -> ExtensionArray | np .ndarray :
13191318 """
13201319 try to cast the array/value to a datetimelike dtype, converting float
13211320 nan to iNaT
13221321
13231322 We allow a list *only* when dtype is not None.
1323+
1324+ Caller is responsible for handling ExtensionDtype cases.
13241325 """
13251326 from pandas .core .arrays .datetimes import sequence_to_datetimes
13261327 from pandas .core .arrays .timedeltas import TimedeltaArray
@@ -1332,18 +1333,22 @@ def maybe_cast_to_datetime(
13321333 # TODO: _from_sequence would raise ValueError in cases where
13331334 # _ensure_nanosecond_dtype raises TypeError
13341335 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]
13361339 res = TimedeltaArray ._from_sequence (value , dtype = dtype )
13371340 return res
13381341
13391342 if dtype is not None :
13401343 is_datetime64 = is_datetime64_dtype (dtype )
1341- is_datetime64tz = is_datetime64tz_dtype (dtype )
13421344
13431345 vdtype = getattr (value , "dtype" , None )
13441346
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]
13471352
13481353 value = np .array (value , copy = False )
13491354
@@ -1352,59 +1357,22 @@ def maybe_cast_to_datetime(
13521357 _disallow_mismatched_datetimelike (value , dtype )
13531358
13541359 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+
14081376 except OutOfBoundsDatetime :
14091377 raise
14101378 except ParserError :
0 commit comments