|
7 | 7 | import inspect
|
8 | 8 | from typing import (
|
9 | 9 | TYPE_CHECKING,
|
10 |
| - cast, |
11 | 10 | overload,
|
12 | 11 | )
|
13 |
| -import warnings |
14 | 12 |
|
15 | 13 | import numpy as np
|
16 | 14 |
|
|
27 | 25 | IgnoreRaise,
|
28 | 26 | )
|
29 | 27 | from pandas.errors import IntCastingNaNError
|
30 |
| -from pandas.util._exceptions import find_stack_level |
31 | 28 |
|
32 | 29 | from pandas.core.dtypes.common import (
|
33 | 30 | is_datetime64_dtype,
|
|
39 | 36 | pandas_dtype,
|
40 | 37 | )
|
41 | 38 | from pandas.core.dtypes.dtypes import (
|
42 |
| - DatetimeTZDtype, |
43 | 39 | ExtensionDtype,
|
44 | 40 | PandasDtype,
|
45 | 41 | )
|
46 | 42 | from pandas.core.dtypes.missing import isna
|
47 | 43 |
|
48 | 44 | if TYPE_CHECKING:
|
49 |
| - from pandas.core.arrays import ( |
50 |
| - DatetimeArray, |
51 |
| - ExtensionArray, |
52 |
| - ) |
| 45 | + from pandas.core.arrays import ExtensionArray |
53 | 46 |
|
54 | 47 |
|
55 | 48 | _dtype_obj = np.dtype(object)
|
@@ -227,7 +220,13 @@ def astype_array(values: ArrayLike, dtype: DtypeObj, copy: bool = False) -> Arra
|
227 | 220 | raise TypeError(msg)
|
228 | 221 |
|
229 | 222 | if is_datetime64tz_dtype(dtype) and is_datetime64_dtype(values.dtype):
|
230 |
| - return astype_dt64_to_dt64tz(values, dtype, copy, via_utc=True) |
| 223 | + # Series.astype behavior pre-2.0 did |
| 224 | + # values.tz_localize("UTC").tz_convert(dtype.tz) |
| 225 | + # which did not match the DTA/DTI behavior. |
| 226 | + raise TypeError( |
| 227 | + "Cannot use .astype to convert from timezone-naive dtype to " |
| 228 | + "timezone-aware dtype. Use ser.dt.tz_localize instead." |
| 229 | + ) |
231 | 230 |
|
232 | 231 | if is_dtype_equal(values.dtype, dtype):
|
233 | 232 | if copy:
|
@@ -351,80 +350,3 @@ def astype_td64_unit_conversion(
|
351 | 350 | mask = isna(values)
|
352 | 351 | np.putmask(result, mask, np.nan)
|
353 | 352 | return result
|
354 |
| - |
355 |
| - |
356 |
| -def astype_dt64_to_dt64tz( |
357 |
| - values: ArrayLike, dtype: DtypeObj, copy: bool, via_utc: bool = False |
358 |
| -) -> DatetimeArray: |
359 |
| - # GH#33401 we have inconsistent behaviors between |
360 |
| - # Datetimeindex[naive].astype(tzaware) |
361 |
| - # Series[dt64].astype(tzaware) |
362 |
| - # This collects them in one place to prevent further fragmentation. |
363 |
| - |
364 |
| - from pandas.core.construction import ensure_wrapped_if_datetimelike |
365 |
| - |
366 |
| - values = ensure_wrapped_if_datetimelike(values) |
367 |
| - values = cast("DatetimeArray", values) |
368 |
| - aware = isinstance(dtype, DatetimeTZDtype) |
369 |
| - |
370 |
| - if via_utc: |
371 |
| - # Series.astype behavior |
372 |
| - |
373 |
| - # caller is responsible for checking this |
374 |
| - assert values.tz is None and aware |
375 |
| - dtype = cast(DatetimeTZDtype, dtype) |
376 |
| - |
377 |
| - if copy: |
378 |
| - # this should be the only copy |
379 |
| - values = values.copy() |
380 |
| - |
381 |
| - warnings.warn( |
382 |
| - "Using .astype to convert from timezone-naive dtype to " |
383 |
| - "timezone-aware dtype is deprecated and will raise in a " |
384 |
| - "future version. Use ser.dt.tz_localize instead.", |
385 |
| - FutureWarning, |
386 |
| - stacklevel=find_stack_level(), |
387 |
| - ) |
388 |
| - |
389 |
| - # GH#33401 this doesn't match DatetimeArray.astype, which |
390 |
| - # goes through the `not via_utc` path |
391 |
| - return values.tz_localize("UTC").tz_convert(dtype.tz) |
392 |
| - |
393 |
| - else: |
394 |
| - # DatetimeArray/DatetimeIndex.astype behavior |
395 |
| - if values.tz is None and aware: |
396 |
| - dtype = cast(DatetimeTZDtype, dtype) |
397 |
| - warnings.warn( |
398 |
| - "Using .astype to convert from timezone-naive dtype to " |
399 |
| - "timezone-aware dtype is deprecated and will raise in a " |
400 |
| - "future version. Use obj.tz_localize instead.", |
401 |
| - FutureWarning, |
402 |
| - stacklevel=find_stack_level(), |
403 |
| - ) |
404 |
| - |
405 |
| - return values.tz_localize(dtype.tz) |
406 |
| - |
407 |
| - elif aware: |
408 |
| - # GH#18951: datetime64_tz dtype but not equal means different tz |
409 |
| - dtype = cast(DatetimeTZDtype, dtype) |
410 |
| - result = values.tz_convert(dtype.tz) |
411 |
| - if copy: |
412 |
| - result = result.copy() |
413 |
| - return result |
414 |
| - |
415 |
| - elif values.tz is not None: |
416 |
| - warnings.warn( |
417 |
| - "Using .astype to convert from timezone-aware dtype to " |
418 |
| - "timezone-naive dtype is deprecated and will raise in a " |
419 |
| - "future version. Use obj.tz_localize(None) or " |
420 |
| - "obj.tz_convert('UTC').tz_localize(None) instead", |
421 |
| - FutureWarning, |
422 |
| - stacklevel=find_stack_level(), |
423 |
| - ) |
424 |
| - |
425 |
| - result = values.tz_convert("UTC").tz_localize(None) |
426 |
| - if copy: |
427 |
| - result = result.copy() |
428 |
| - return result |
429 |
| - |
430 |
| - raise NotImplementedError("dtype_equal case should be handled elsewhere") |
0 commit comments