Skip to content

Commit ac9901b

Browse files
jbrockmendeljreback
authored andcommitted
REF: move astype to ExtensionIndex (#30800)
1 parent df88954 commit ac9901b

File tree

4 files changed

+15
-22
lines changed

4 files changed

+15
-22
lines changed

pandas/core/indexes/category.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,7 @@ def _engine_type(self):
190190
# Constructors
191191

192192
def __new__(
193-
cls,
194-
data=None,
195-
categories=None,
196-
ordered=None,
197-
dtype=None,
198-
copy=False,
199-
name=None,
193+
cls, data=None, categories=None, ordered=None, dtype=None, copy=False, name=None
200194
):
201195

202196
dtype = CategoricalDtype._from_values_or_dtype(data, categories, ordered, dtype)
@@ -416,7 +410,7 @@ def astype(self, dtype, copy=True):
416410
if dtype == self.dtype:
417411
return self.copy() if copy else self
418412

419-
return super().astype(dtype=dtype, copy=copy)
413+
return Index.astype(self, dtype=dtype, copy=copy)
420414

421415
@cache_readonly
422416
def _isnan(self):

pandas/core/indexes/datetimelike.py

-12
Original file line numberDiff line numberDiff line change
@@ -542,18 +542,6 @@ def _concat_same_dtype(self, to_concat, name):
542542

543543
return self._simple_new(new_data, **attribs)
544544

545-
@Appender(_index_shared_docs["astype"])
546-
def astype(self, dtype, copy=True):
547-
if is_dtype_equal(self.dtype, dtype) and copy is False:
548-
# Ensure that self.astype(self.dtype) is self
549-
return self
550-
551-
new_values = self._data.astype(dtype, copy=copy)
552-
553-
# pass copy=False because any copying will be done in the
554-
# _data.astype call above
555-
return Index(new_values, dtype=new_values.dtype, name=self.name, copy=False)
556-
557545
def shift(self, periods=1, freq=None):
558546
"""
559547
Shift index by desired number of time frequency increments.

pandas/core/indexes/extension.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from pandas.compat.numpy import function as nv
77
from pandas.util._decorators import cache_readonly
88

9-
from pandas.core.dtypes.common import ensure_platform_int
9+
from pandas.core.dtypes.common import ensure_platform_int, is_dtype_equal
1010
from pandas.core.dtypes.generic import ABCSeries
1111

1212
from pandas.core.arrays import ExtensionArray
@@ -192,3 +192,14 @@ def _get_unique_index(self, dropna=False):
192192
if dropna and self.hasnans:
193193
result = result[~result.isna()]
194194
return self._shallow_copy(result)
195+
196+
def astype(self, dtype, copy=True):
197+
if is_dtype_equal(self.dtype, dtype) and copy is False:
198+
# Ensure that self.astype(self.dtype) is self
199+
return self
200+
201+
new_values = self._data.astype(dtype, copy=copy)
202+
203+
# pass copy=False because any copying will be done in the
204+
# _data.astype call above
205+
return Index(new_values, dtype=new_values.dtype, name=self.name, copy=False)

pandas/core/indexes/interval.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ def astype(self, dtype, copy=True):
426426
new_values = self.values.astype(dtype, copy=copy)
427427
if is_interval_dtype(new_values):
428428
return self._shallow_copy(new_values.left, new_values.right)
429-
return super().astype(dtype, copy=copy)
429+
return Index.astype(self, dtype, copy=copy)
430430

431431
@property
432432
def inferred_type(self) -> str:

0 commit comments

Comments
 (0)