Skip to content

CLN: DatetimeArray.astype #38572

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
is_categorical_dtype,
is_datetime64_any_dtype,
is_datetime64_dtype,
is_datetime64_ns_dtype,
is_datetime64tz_dtype,
is_dtype_equal,
is_extension_array_dtype,
Expand Down Expand Up @@ -587,29 +586,30 @@ def astype(self, dtype, copy=True):
# DatetimeLikeArrayMixin Super handles the rest.
dtype = pandas_dtype(dtype)

if is_datetime64_ns_dtype(dtype) and not is_dtype_equal(dtype, self.dtype):
# GH#18951: datetime64_ns dtype but not equal means different tz
# FIXME: this doesn't match DatetimeBlock.astype, xref GH#33401
new_tz = getattr(dtype, "tz", None)
if self.tz is None:
return self.tz_localize(new_tz)
elif new_tz is None:
result = self.tz_convert("UTC").tz_localize(None)
else:
result = self.tz_convert(new_tz)
if is_dtype_equal(dtype, self.dtype):
if copy:
return self.copy()
return self

elif is_datetime64tz_dtype(dtype) and self.tz is None:
# FIXME: GH#33401 this does not match Series behavior
return self.tz_localize(dtype.tz)

elif is_datetime64tz_dtype(dtype):
# GH#18951: datetime64_ns dtype but not equal means different tz
result = self.tz_convert(dtype.tz)
if copy:
result = result.copy()
if new_tz is None:
# Do we want .astype('datetime64[ns]') to be an ndarray.
# The astype in Block._astype expects this to return an
# ndarray, but we could maybe work around it there.
result = result._data
return result
elif is_datetime64tz_dtype(self.dtype) and is_dtype_equal(self.dtype, dtype):

elif dtype == "M8[ns]":
# we must have self.tz is None, otherwise we would have gone through
# the is_dtype_equal branch above.
result = self.tz_convert("UTC").tz_localize(None)
if copy:
return self.copy()
return self
result = result.copy()
return result

elif is_period_dtype(dtype):
return self.to_period(freq=dtype.freq)
return dtl.DatetimeLikeArrayMixin.astype(self, dtype, copy)
Expand Down
7 changes: 1 addition & 6 deletions pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,12 +324,7 @@ def astype(self, dtype, copy: bool = True):
# DatetimeLikeArrayMixin super call handles other cases
dtype = pandas_dtype(dtype)

if is_dtype_equal(dtype, self.dtype):
if copy:
return self.copy()
return self

elif dtype.kind == "m":
if dtype.kind == "m":
return astype_td64_unit_conversion(self._data, dtype, copy=copy)

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