Skip to content

Commit 526519f

Browse files
jbrockmendelluckyvs1
authored andcommitted
CLN: DatetimeArray.astype (pandas-dev#38572)
1 parent b1220ac commit 526519f

File tree

2 files changed

+20
-25
lines changed

2 files changed

+20
-25
lines changed

pandas/core/arrays/datetimes.py

+19-19
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
is_categorical_dtype,
3232
is_datetime64_any_dtype,
3333
is_datetime64_dtype,
34-
is_datetime64_ns_dtype,
3534
is_datetime64tz_dtype,
3635
is_dtype_equal,
3736
is_extension_array_dtype,
@@ -587,29 +586,30 @@ def astype(self, dtype, copy=True):
587586
# DatetimeLikeArrayMixin Super handles the rest.
588587
dtype = pandas_dtype(dtype)
589588

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)
600597

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)
601601
if copy:
602602
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
608603
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)
610609
if copy:
611-
return self.copy()
612-
return self
610+
result = result.copy()
611+
return result
612+
613613
elif is_period_dtype(dtype):
614614
return self.to_period(freq=dtype.freq)
615615
return dtl.DatetimeLikeArrayMixin.astype(self, dtype, copy)

pandas/core/arrays/timedeltas.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -324,12 +324,7 @@ def astype(self, dtype, copy: bool = True):
324324
# DatetimeLikeArrayMixin super call handles other cases
325325
dtype = pandas_dtype(dtype)
326326

327-
if is_dtype_equal(dtype, self.dtype):
328-
if copy:
329-
return self.copy()
330-
return self
331-
332-
elif dtype.kind == "m":
327+
if dtype.kind == "m":
333328
return astype_td64_unit_conversion(self._data, dtype, copy=copy)
334329

335330
return dtl.DatetimeLikeArrayMixin.astype(self, dtype, copy=copy)

0 commit comments

Comments
 (0)