Skip to content

Commit 7043f8f

Browse files
authored
REF: use astype_nansafe in Index.astype (#38518)
1 parent d210962 commit 7043f8f

File tree

2 files changed

+10
-29
lines changed

2 files changed

+10
-29
lines changed

pandas/core/indexes/base.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from pandas.util._decorators import Appender, cache_readonly, doc
3434

3535
from pandas.core.dtypes.cast import (
36+
astype_nansafe,
3637
find_common_type,
3738
maybe_cast_to_integer_array,
3839
maybe_promote,
@@ -693,22 +694,21 @@ def astype(self, dtype, copy=True):
693694
if is_dtype_equal(self.dtype, dtype):
694695
return self.copy() if copy else self
695696

696-
elif is_categorical_dtype(dtype):
697-
from pandas.core.indexes.category import CategoricalIndex
698-
699-
return CategoricalIndex(
700-
self._values, name=self.name, dtype=dtype, copy=copy
697+
if needs_i8_conversion(dtype) and is_float_dtype(self.dtype):
698+
# We can't put this into astype_nansafe bc astype_nansafe allows
699+
# casting np.nan to NaT
700+
raise TypeError(
701+
f"Cannot convert {type(self).__name__} to dtype {dtype}; integer "
702+
"values are required for conversion"
701703
)
702704

703-
elif is_extension_array_dtype(dtype):
704-
return Index(np.asarray(self), name=self.name, dtype=dtype, copy=copy)
705-
706705
try:
707-
casted = self._values.astype(dtype, copy=copy)
708-
except (TypeError, ValueError) as err:
706+
casted = astype_nansafe(self._values, dtype=dtype, copy=True)
707+
except TypeError as err:
709708
raise TypeError(
710709
f"Cannot cast {type(self).__name__} to dtype {dtype}"
711710
) from err
711+
712712
return Index(casted, name=self.name, dtype=dtype)
713713

714714
_index_shared_docs[

pandas/core/indexes/numeric.py

-19
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77
from pandas._typing import Dtype, DtypeObj, Label
88
from pandas.util._decorators import doc
99

10-
from pandas.core.dtypes.cast import astype_nansafe
1110
from pandas.core.dtypes.common import (
1211
is_bool,
1312
is_bool_dtype,
1413
is_dtype_equal,
15-
is_extension_array_dtype,
1614
is_float,
1715
is_float_dtype,
1816
is_integer_dtype,
@@ -21,8 +19,6 @@
2119
is_scalar,
2220
is_signed_integer_dtype,
2321
is_unsigned_integer_dtype,
24-
needs_i8_conversion,
25-
pandas_dtype,
2622
)
2723
from pandas.core.dtypes.generic import ABCSeries
2824
from pandas.core.dtypes.missing import is_valid_nat_for_dtype, isna
@@ -332,21 +328,6 @@ def inferred_type(self) -> str:
332328
"""
333329
return "floating"
334330

335-
@doc(Index.astype)
336-
def astype(self, dtype, copy=True):
337-
dtype = pandas_dtype(dtype)
338-
if needs_i8_conversion(dtype):
339-
raise TypeError(
340-
f"Cannot convert Float64Index to dtype {dtype}; integer "
341-
"values are required for conversion"
342-
)
343-
elif is_integer_dtype(dtype) and not is_extension_array_dtype(dtype):
344-
# TODO(jreback); this can change once we have an EA Index type
345-
# GH 13149
346-
arr = astype_nansafe(self._values, dtype=dtype)
347-
return Int64Index(arr, name=self.name)
348-
return super().astype(dtype, copy=copy)
349-
350331
# ----------------------------------------------------------------
351332
# Indexing Methods
352333

0 commit comments

Comments
 (0)