Skip to content

Commit 7c7c0dd

Browse files
committed
address comments
1 parent 951c5f7 commit 7c7c0dd

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

pandas/core/indexes/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,8 @@ def _outer_indexer(
361361
_can_hold_strings: bool = True
362362

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

367368
_engine_type: type[libindex.IndexEngine] = libindex.ObjectEngine

pandas/core/indexes/category.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ def _is_dtype_compat(self, other) -> Categorical:
282282
return other
283283

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

288288
dtype = pandas_dtype(dtype)

pandas/core/indexes/numeric.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ def _ensure_array(cls, data, dtype, copy: bool):
166166
dtype = cls._ensure_dtype(dtype)
167167

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

0 commit comments

Comments
 (0)