Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
topper-123 committed Jul 28, 2021
1 parent 9f2138c commit d804bb6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
3 changes: 2 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,8 @@ def _outer_indexer(
_can_hold_strings: bool = True

# Whether this index is a NumericIndex, but not a Int64Index, Float64Index,
# UInt64Index or RangeIndex. Needed for backwards compat. Remove in pandas 2.0.
# UInt64Index or RangeIndex. Needed for backwards compat. Remove this attribute and
# associated code in pandas 2.0.
_is_backward_compat_public_numeric_index: bool = False

_engine_type: type[libindex.IndexEngine] = libindex.ObjectEngine
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ def _is_dtype_compat(self, other) -> Categorical:
return other

@doc(Index.astype)
def astype(self, dtype, copy: bool = True) -> Index:
def astype(self, dtype: Dtype, copy: bool = True) -> Index:
from pandas.core.api import NumericIndex

dtype = pandas_dtype(dtype)
Expand Down
7 changes: 7 additions & 0 deletions pandas/core/indexes/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ def _ensure_array(cls, data, dtype, copy: bool):
dtype = cls._ensure_dtype(dtype)

if copy or not is_dtype_equal(data.dtype, dtype):
# the try/except below is because it's difficult to predict the error
# and/or error message from different combinations of data and type.
# Efforts to avoid this try/except welcome.
# See https://github.com/pandas-dev/pandas/pull/41153#discussion_r676206222
try:
subarr = np.array(data, dtype=dtype, copy=copy)
cls._validate_dtype(subarr.dtype)
Expand Down Expand Up @@ -247,6 +251,9 @@ def astype(self, dtype, copy=True):
else:
return NumericIndex(arr, name=self.name, dtype=dtype)
elif self._is_backward_compat_public_numeric_index:
# this block is needed so e.g. NumericIndex[int8].astype("int32") returns
# NumericIndex[int32] and not Int64Index with dtype int64.
# When Int64Index etc. are removed from the code base, removed this also.
if not is_extension_array_dtype(dtype) and is_numeric_dtype(dtype):
return self._constructor(self, dtype=dtype, copy=copy)

Expand Down

0 comments on commit d804bb6

Please sign in to comment.