|
91 | 91 | ensure_platform_int, |
92 | 92 | is_bool_dtype, |
93 | 93 | is_categorical_dtype, |
| 94 | + is_complex_dtype, |
94 | 95 | is_dtype_equal, |
95 | 96 | is_ea_or_datetimelike_dtype, |
96 | 97 | is_extension_array_dtype, |
@@ -590,18 +591,14 @@ def _dtype_to_subclass(cls, dtype: DtypeObj): |
590 | 591 |
|
591 | 592 | return TimedeltaIndex |
592 | 593 |
|
593 | | - elif dtype.kind == "f": |
594 | | - from pandas.core.api import Float64Index |
595 | | - |
596 | | - return Float64Index |
597 | | - elif dtype.kind == "u": |
598 | | - from pandas.core.api import UInt64Index |
599 | | - |
600 | | - return UInt64Index |
601 | | - elif dtype.kind == "i": |
602 | | - from pandas.core.api import Int64Index |
| 594 | + elif ( |
| 595 | + is_numeric_dtype(dtype) |
| 596 | + and not is_bool_dtype(dtype) |
| 597 | + and not is_complex_dtype(dtype) |
| 598 | + ): |
| 599 | + from pandas.core.api import NumericIndex |
603 | 600 |
|
604 | | - return Int64Index |
| 601 | + return NumericIndex |
605 | 602 |
|
606 | 603 | elif dtype.kind == "O": |
607 | 604 | # NB: assuming away MultiIndex |
@@ -1072,14 +1069,29 @@ def astype(self, dtype, copy: bool = True): |
1072 | 1069 | ) from err |
1073 | 1070 |
|
1074 | 1071 | # pass copy=False because any copying will be done in the astype above |
1075 | | - if self._is_backward_compat_public_numeric_index: |
1076 | | - # this block is needed so e.g. NumericIndex[int8].astype("int32") returns |
1077 | | - # NumericIndex[int32] and not Int64Index with dtype int64. |
| 1072 | + if not self._is_backward_compat_public_numeric_index and not isinstance( |
| 1073 | + self, ABCRangeIndex |
| 1074 | + ): |
| 1075 | + # this block is needed so e.g. Int64Index.astype("int32") returns |
| 1076 | + # Int64Index and not a NumericIndex with dtype int32. |
1078 | 1077 | # When Int64Index etc. are removed from the code base, removed this also. |
1079 | 1078 | if isinstance(dtype, np.dtype) and is_numeric_dtype(dtype): |
1080 | | - return self._constructor( |
1081 | | - new_values, name=self.name, dtype=dtype, copy=False |
| 1079 | + from pandas.core.api import ( |
| 1080 | + Float64Index, |
| 1081 | + Int64Index, |
| 1082 | + UInt64Index, |
1082 | 1083 | ) |
| 1084 | + |
| 1085 | + if is_signed_integer_dtype(dtype): |
| 1086 | + klass = Int64Index |
| 1087 | + elif is_unsigned_integer_dtype(dtype): |
| 1088 | + klass = UInt64Index |
| 1089 | + elif is_float_dtype(dtype): |
| 1090 | + klass = Float64Index |
| 1091 | + else: |
| 1092 | + klass = Index |
| 1093 | + return klass(new_values, name=self.name, dtype=dtype, copy=False) |
| 1094 | + |
1083 | 1095 | return Index(new_values, name=self.name, dtype=new_values.dtype, copy=False) |
1084 | 1096 |
|
1085 | 1097 | _index_shared_docs[ |
@@ -5328,6 +5340,7 @@ def putmask(self, mask, value) -> Index: |
5328 | 5340 | if self.dtype != object and is_valid_na_for_dtype(value, self.dtype): |
5329 | 5341 | # e.g. None -> np.nan, see also Block._standardize_fill_value |
5330 | 5342 | value = self._na_value |
| 5343 | + |
5331 | 5344 | try: |
5332 | 5345 | converted = self._validate_fill_value(value) |
5333 | 5346 | except (LossySetitemError, ValueError, TypeError) as err: |
@@ -6196,13 +6209,6 @@ def map(self, mapper, na_action=None): |
6196 | 6209 | new_values, self.dtype, same_dtype=same_dtype |
6197 | 6210 | ) |
6198 | 6211 |
|
6199 | | - if self._is_backward_compat_public_numeric_index and is_numeric_dtype( |
6200 | | - new_values.dtype |
6201 | | - ): |
6202 | | - return self._constructor( |
6203 | | - new_values, dtype=dtype, copy=False, name=self.name |
6204 | | - ) |
6205 | | - |
6206 | 6212 | return Index._with_infer(new_values, dtype=dtype, copy=False, name=self.name) |
6207 | 6213 |
|
6208 | 6214 | # TODO: De-duplicate with map, xref GH#32349 |
@@ -6679,10 +6685,17 @@ def insert(self, loc: int, item) -> Index: |
6679 | 6685 | loc = loc if loc >= 0 else loc - 1 |
6680 | 6686 | new_values[loc] = item |
6681 | 6687 |
|
6682 | | - if self._typ == "numericindex": |
6683 | | - # Use self._constructor instead of Index to retain NumericIndex GH#43921 |
6684 | | - # TODO(2.0) can use Index instead of self._constructor |
6685 | | - return self._constructor._with_infer(new_values, name=self.name) |
| 6688 | + if not self._is_backward_compat_public_numeric_index: |
| 6689 | + from pandas.core.indexes.numeric import NumericIndex |
| 6690 | + |
| 6691 | + if not isinstance(self, ABCRangeIndex) or not isinstance( |
| 6692 | + self, NumericIndex |
| 6693 | + ): |
| 6694 | + return Index._with_infer(new_values, name=self.name) |
| 6695 | + else: |
| 6696 | + # Use self._constructor instead of Index to retain old-style num. index |
| 6697 | + # TODO(2.0) can use Index instead of self._constructor |
| 6698 | + return self._constructor._with_infer(new_values, name=self.name) |
6686 | 6699 | else: |
6687 | 6700 | return Index._with_infer(new_values, name=self.name) |
6688 | 6701 |
|
|
0 commit comments