Skip to content

REF: move astype to ExtensionIndex #30800

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
Jan 8, 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
10 changes: 2 additions & 8 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,7 @@ def _engine_type(self):
# Constructors

def __new__(
cls,
data=None,
categories=None,
ordered=None,
dtype=None,
copy=False,
name=None,
cls, data=None, categories=None, ordered=None, dtype=None, copy=False, name=None
):

dtype = CategoricalDtype._from_values_or_dtype(data, categories, ordered, dtype)
Expand Down Expand Up @@ -414,7 +408,7 @@ def astype(self, dtype, copy=True):
if dtype == self.dtype:
return self.copy() if copy else self

return super().astype(dtype=dtype, copy=copy)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a consistent reason to use Index instead of super?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC i got test failures without this change

return Index.astype(self, dtype=dtype, copy=copy)

@cache_readonly
def _isnan(self):
Expand Down
12 changes: 0 additions & 12 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,18 +542,6 @@ def _concat_same_dtype(self, to_concat, name):

return self._simple_new(new_data, **attribs)

@Appender(_index_shared_docs["astype"])
def astype(self, dtype, copy=True):
if is_dtype_equal(self.dtype, dtype) and copy is False:
# Ensure that self.astype(self.dtype) is self
return self

new_values = self._data.astype(dtype, copy=copy)

# pass copy=False because any copying will be done in the
# _data.astype call above
return Index(new_values, dtype=new_values.dtype, name=self.name, copy=False)

def shift(self, periods=1, freq=None):
"""
Shift index by desired number of time frequency increments.
Expand Down
13 changes: 12 additions & 1 deletion pandas/core/indexes/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pandas.compat.numpy import function as nv
from pandas.util._decorators import cache_readonly

from pandas.core.dtypes.common import ensure_platform_int
from pandas.core.dtypes.common import ensure_platform_int, is_dtype_equal
from pandas.core.dtypes.generic import ABCSeries

from pandas.core.arrays import ExtensionArray
Expand Down Expand Up @@ -192,3 +192,14 @@ def _get_unique_index(self, dropna=False):
if dropna and self.hasnans:
result = result[~result.isna()]
return self._shallow_copy(result)

def astype(self, dtype, copy=True):
if is_dtype_equal(self.dtype, dtype) and copy is False:
# Ensure that self.astype(self.dtype) is self
return self

new_values = self._data.astype(dtype, copy=copy)

# pass copy=False because any copying will be done in the
# _data.astype call above
return Index(new_values, dtype=new_values.dtype, name=self.name, copy=False)
2 changes: 1 addition & 1 deletion pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ def astype(self, dtype, copy=True):
new_values = self.values.astype(dtype, copy=copy)
if is_interval_dtype(new_values):
return self._shallow_copy(new_values.left, new_values.right)
return super().astype(dtype, copy=copy)
return Index.astype(self, dtype, copy=copy)

@property
def inferred_type(self) -> str:
Expand Down