|
31 | 31 | is_categorical_dtype,
|
32 | 32 | is_datetime64_any_dtype,
|
33 | 33 | is_datetime64_dtype,
|
34 |
| - is_datetime64_ns_dtype, |
35 | 34 | is_datetime64tz_dtype,
|
36 | 35 | is_dtype_equal,
|
37 | 36 | is_extension_array_dtype,
|
@@ -587,29 +586,30 @@ def astype(self, dtype, copy=True):
|
587 | 586 | # DatetimeLikeArrayMixin Super handles the rest.
|
588 | 587 | dtype = pandas_dtype(dtype)
|
589 | 588 |
|
590 |
| - if is_datetime64_ns_dtype(dtype) and not is_dtype_equal(dtype, self.dtype): |
591 |
| - # GH#18951: datetime64_ns dtype but not equal means different tz |
592 |
| - # FIXME: this doesn't match DatetimeBlock.astype, xref GH#33401 |
593 |
| - new_tz = getattr(dtype, "tz", None) |
594 |
| - if self.tz is None: |
595 |
| - return self.tz_localize(new_tz) |
596 |
| - elif new_tz is None: |
597 |
| - result = self.tz_convert("UTC").tz_localize(None) |
598 |
| - else: |
599 |
| - result = self.tz_convert(new_tz) |
| 589 | + if is_dtype_equal(dtype, self.dtype): |
| 590 | + if copy: |
| 591 | + return self.copy() |
| 592 | + return self |
| 593 | + |
| 594 | + elif is_datetime64tz_dtype(dtype) and self.tz is None: |
| 595 | + # FIXME: GH#33401 this does not match Series behavior |
| 596 | + return self.tz_localize(dtype.tz) |
600 | 597 |
|
| 598 | + elif is_datetime64tz_dtype(dtype): |
| 599 | + # GH#18951: datetime64_ns dtype but not equal means different tz |
| 600 | + result = self.tz_convert(dtype.tz) |
601 | 601 | if copy:
|
602 | 602 | result = result.copy()
|
603 |
| - if new_tz is None: |
604 |
| - # Do we want .astype('datetime64[ns]') to be an ndarray. |
605 |
| - # The astype in Block._astype expects this to return an |
606 |
| - # ndarray, but we could maybe work around it there. |
607 |
| - result = result._data |
608 | 603 | return result
|
609 |
| - elif is_datetime64tz_dtype(self.dtype) and is_dtype_equal(self.dtype, dtype): |
| 604 | + |
| 605 | + elif dtype == "M8[ns]": |
| 606 | + # we must have self.tz is None, otherwise we would have gone through |
| 607 | + # the is_dtype_equal branch above. |
| 608 | + result = self.tz_convert("UTC").tz_localize(None) |
610 | 609 | if copy:
|
611 |
| - return self.copy() |
612 |
| - return self |
| 610 | + result = result.copy() |
| 611 | + return result |
| 612 | + |
613 | 613 | elif is_period_dtype(dtype):
|
614 | 614 | return self.to_period(freq=dtype.freq)
|
615 | 615 | return dtl.DatetimeLikeArrayMixin.astype(self, dtype, copy)
|
|
0 commit comments