Skip to content

Commit e4892f3

Browse files
authored
REF: roll DatetimeBlock.astype into Block._astype (#38460)
1 parent e3443e3 commit e4892f3

File tree

1 file changed

+10
-19
lines changed

1 file changed

+10
-19
lines changed

pandas/core/internals/blocks.py

+10-19
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,16 @@ def _astype(self, dtype: DtypeObj, copy: bool) -> ArrayLike:
654654

655655
return Categorical(values, dtype=dtype)
656656

657+
elif is_datetime64tz_dtype(dtype) and is_datetime64_dtype(values.dtype):
658+
# if we are passed a datetime64[ns, tz]
659+
if copy:
660+
# this should be the only copy
661+
values = values.copy()
662+
# i.e. values.tz_localize("UTC").tz_convert(dtype.tz)
663+
# FIXME: GH#33401 this doesn't match DatetimeArray.astype, which
664+
# would be self.array_values().tz_localize(dtype.tz)
665+
return DatetimeArray._simple_new(values.view("i8"), dtype=dtype)
666+
657667
if is_dtype_equal(values.dtype, dtype):
658668
if copy:
659669
return values.copy()
@@ -2237,25 +2247,6 @@ def _maybe_coerce_values(self, values):
22372247
assert isinstance(values, np.ndarray), type(values)
22382248
return values
22392249

2240-
def astype(self, dtype, copy: bool = False, errors: str = "raise"):
2241-
"""
2242-
these automatically copy, so copy=True has no effect
2243-
raise on an except if raise == True
2244-
"""
2245-
dtype = pandas_dtype(dtype)
2246-
2247-
# if we are passed a datetime64[ns, tz]
2248-
if is_datetime64tz_dtype(dtype):
2249-
values = self.values
2250-
if copy:
2251-
# this should be the only copy
2252-
values = values.copy()
2253-
values = DatetimeArray._simple_new(values.view("i8"), dtype=dtype)
2254-
return self.make_block(values)
2255-
2256-
# delegate
2257-
return super().astype(dtype=dtype, copy=copy, errors=errors)
2258-
22592250
def _can_hold_element(self, element: Any) -> bool:
22602251
tipo = maybe_infer_dtype_type(element)
22612252
if tipo is not None:

0 commit comments

Comments
 (0)