Skip to content

Commit dc87874

Browse files
Revert "REF: use astype_nansafe in Index.astype (#38518)"
This reverts commit 7043f8f.
1 parent b7c994b commit dc87874

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

pandas/core/indexes/base.py

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

3535
from pandas.core.dtypes.cast import (
36-
astype_nansafe,
3736
find_common_type,
3837
maybe_cast_to_integer_array,
3938
maybe_promote,
@@ -694,21 +693,22 @@ def astype(self, dtype, copy=True):
694693
if is_dtype_equal(self.dtype, dtype):
695694
return self.copy() if copy else self
696695

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"
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
703701
)
704702

703+
elif is_extension_array_dtype(dtype):
704+
return Index(np.asarray(self), name=self.name, dtype=dtype, copy=copy)
705+
705706
try:
706-
casted = astype_nansafe(self._values, dtype=dtype, copy=True)
707-
except TypeError as err:
707+
casted = self._values.astype(dtype, copy=copy)
708+
except (TypeError, ValueError) as err:
708709
raise TypeError(
709710
f"Cannot cast {type(self).__name__} to dtype {dtype}"
710711
) 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,10 +7,12 @@
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
1011
from pandas.core.dtypes.common import (
1112
is_bool,
1213
is_bool_dtype,
1314
is_dtype_equal,
15+
is_extension_array_dtype,
1416
is_float,
1517
is_float_dtype,
1618
is_integer_dtype,
@@ -19,6 +21,8 @@
1921
is_scalar,
2022
is_signed_integer_dtype,
2123
is_unsigned_integer_dtype,
24+
needs_i8_conversion,
25+
pandas_dtype,
2226
)
2327
from pandas.core.dtypes.generic import ABCSeries
2428
from pandas.core.dtypes.missing import is_valid_nat_for_dtype, isna
@@ -328,6 +332,21 @@ def inferred_type(self) -> str:
328332
"""
329333
return "floating"
330334

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+
331350
# ----------------------------------------------------------------
332351
# Indexing Methods
333352

0 commit comments

Comments
 (0)