Skip to content

REF: roll DatetimeBlock.astype into Block._astype #38460

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 1 commit into from
Dec 14, 2020
Merged
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
29 changes: 10 additions & 19 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,16 @@ def _astype(self, dtype: DtypeObj, copy: bool) -> ArrayLike:

return Categorical(values, dtype=dtype)

elif is_datetime64tz_dtype(dtype) and is_datetime64_dtype(values.dtype):
# if we are passed a datetime64[ns, tz]
if copy:
# this should be the only copy
values = values.copy()
# i.e. values.tz_localize("UTC").tz_convert(dtype.tz)
# FIXME: GH#33401 this doesn't match DatetimeArray.astype, which
# would be self.array_values().tz_localize(dtype.tz)
return DatetimeArray._simple_new(values.view("i8"), dtype=dtype)

if is_dtype_equal(values.dtype, dtype):
if copy:
return values.copy()
Expand Down Expand Up @@ -2237,25 +2247,6 @@ def _maybe_coerce_values(self, values):
assert isinstance(values, np.ndarray), type(values)
return values

def astype(self, dtype, copy: bool = False, errors: str = "raise"):
"""
these automatically copy, so copy=True has no effect
raise on an except if raise == True
"""
dtype = pandas_dtype(dtype)

# if we are passed a datetime64[ns, tz]
if is_datetime64tz_dtype(dtype):
values = self.values
if copy:
# this should be the only copy
values = values.copy()
values = DatetimeArray._simple_new(values.view("i8"), dtype=dtype)
return self.make_block(values)

# delegate
return super().astype(dtype=dtype, copy=copy, errors=errors)

def _can_hold_element(self, element: Any) -> bool:
tipo = maybe_infer_dtype_type(element)
if tipo is not None:
Expand Down