|
89 | 89 | ensure_platform_int,
|
90 | 90 | is_bool_dtype,
|
91 | 91 | is_categorical_dtype,
|
| 92 | + is_complex_dtype, |
92 | 93 | is_dtype_equal,
|
93 | 94 | is_ea_or_datetimelike_dtype,
|
94 | 95 | is_extension_array_dtype,
|
|
104 | 105 | is_scalar,
|
105 | 106 | is_signed_integer_dtype,
|
106 | 107 | is_string_dtype,
|
| 108 | + is_unsigned_integer_dtype, |
107 | 109 | needs_i8_conversion,
|
108 | 110 | pandas_dtype,
|
109 | 111 | validate_all_hashable,
|
@@ -588,18 +590,14 @@ def _dtype_to_subclass(cls, dtype: DtypeObj):
|
588 | 590 |
|
589 | 591 | return TimedeltaIndex
|
590 | 592 |
|
591 |
| - elif dtype.kind == "f": |
592 |
| - from pandas.core.api import Float64Index |
593 |
| - |
594 |
| - return Float64Index |
595 |
| - elif dtype.kind == "u": |
596 |
| - from pandas.core.api import UInt64Index |
597 |
| - |
598 |
| - return UInt64Index |
599 |
| - elif dtype.kind == "i": |
600 |
| - from pandas.core.api import Int64Index |
| 593 | + elif ( |
| 594 | + is_numeric_dtype(dtype) |
| 595 | + and not is_bool_dtype(dtype) |
| 596 | + and not is_complex_dtype(dtype) |
| 597 | + ): |
| 598 | + from pandas.core.api import NumericIndex |
601 | 599 |
|
602 |
| - return Int64Index |
| 600 | + return NumericIndex |
603 | 601 |
|
604 | 602 | elif dtype.kind == "O":
|
605 | 603 | # NB: assuming away MultiIndex
|
@@ -1040,14 +1038,29 @@ def astype(self, dtype, copy: bool = True):
|
1040 | 1038 | new_values = astype_nansafe(values, dtype=dtype, copy=copy)
|
1041 | 1039 |
|
1042 | 1040 | # pass copy=False because any copying will be done in the astype above
|
1043 |
| - if self._is_backward_compat_public_numeric_index: |
1044 |
| - # this block is needed so e.g. NumericIndex[int8].astype("int32") returns |
1045 |
| - # NumericIndex[int32] and not Int64Index with dtype int64. |
| 1041 | + if not self._is_backward_compat_public_numeric_index and not isinstance( |
| 1042 | + self, ABCRangeIndex |
| 1043 | + ): |
| 1044 | + # this block is needed so e.g. Int64Index.astype("int32") returns |
| 1045 | + # Int64Index and not a NumericIndex with dtype int32. |
1046 | 1046 | # When Int64Index etc. are removed from the code base, removed this also.
|
1047 | 1047 | if isinstance(dtype, np.dtype) and is_numeric_dtype(dtype):
|
1048 |
| - return self._constructor( |
1049 |
| - new_values, name=self.name, dtype=dtype, copy=False |
| 1048 | + from pandas.core.api import ( |
| 1049 | + Float64Index, |
| 1050 | + Int64Index, |
| 1051 | + UInt64Index, |
1050 | 1052 | )
|
| 1053 | + |
| 1054 | + if is_signed_integer_dtype(dtype): |
| 1055 | + klass = Int64Index |
| 1056 | + elif is_unsigned_integer_dtype(dtype): |
| 1057 | + klass = UInt64Index |
| 1058 | + elif is_float_dtype(dtype): |
| 1059 | + klass = Float64Index |
| 1060 | + else: |
| 1061 | + klass = Index |
| 1062 | + return klass(new_values, name=self.name, dtype=dtype, copy=False) |
| 1063 | + |
1051 | 1064 | return Index(new_values, name=self.name, dtype=new_values.dtype, copy=False)
|
1052 | 1065 |
|
1053 | 1066 | _index_shared_docs[
|
@@ -5247,6 +5260,7 @@ def putmask(self, mask, value) -> Index:
|
5247 | 5260 | if self.dtype != object and is_valid_na_for_dtype(value, self.dtype):
|
5248 | 5261 | # e.g. None -> np.nan, see also Block._standardize_fill_value
|
5249 | 5262 | value = self._na_value
|
| 5263 | + |
5250 | 5264 | try:
|
5251 | 5265 | converted = self._validate_fill_value(value)
|
5252 | 5266 | except (LossySetitemError, ValueError, TypeError) as err:
|
@@ -6115,13 +6129,6 @@ def map(self, mapper, na_action=None):
|
6115 | 6129 | new_values, self.dtype, same_dtype=same_dtype
|
6116 | 6130 | )
|
6117 | 6131 |
|
6118 |
| - if self._is_backward_compat_public_numeric_index and is_numeric_dtype( |
6119 |
| - new_values.dtype |
6120 |
| - ): |
6121 |
| - return self._constructor( |
6122 |
| - new_values, dtype=dtype, copy=False, name=self.name |
6123 |
| - ) |
6124 |
| - |
6125 | 6132 | return Index._with_infer(new_values, dtype=dtype, copy=False, name=self.name)
|
6126 | 6133 |
|
6127 | 6134 | # TODO: De-duplicate with map, xref GH#32349
|
@@ -6598,10 +6605,17 @@ def insert(self, loc: int, item) -> Index:
|
6598 | 6605 | loc = loc if loc >= 0 else loc - 1
|
6599 | 6606 | new_values[loc] = item
|
6600 | 6607 |
|
6601 |
| - if self._typ == "numericindex": |
6602 |
| - # Use self._constructor instead of Index to retain NumericIndex GH#43921 |
6603 |
| - # TODO(2.0) can use Index instead of self._constructor |
6604 |
| - return self._constructor._with_infer(new_values, name=self.name) |
| 6608 | + if not self._is_backward_compat_public_numeric_index: |
| 6609 | + from pandas.core.indexes.numeric import NumericIndex |
| 6610 | + |
| 6611 | + if not isinstance(self, ABCRangeIndex) or not isinstance( |
| 6612 | + self, NumericIndex |
| 6613 | + ): |
| 6614 | + return Index._with_infer(new_values, name=self.name) |
| 6615 | + else: |
| 6616 | + # Use self._constructor instead of Index to retain old-style num. index |
| 6617 | + # TODO(2.0) can use Index instead of self._constructor |
| 6618 | + return self._constructor._with_infer(new_values, name=self.name) |
6605 | 6619 | else:
|
6606 | 6620 | return Index._with_infer(new_values, name=self.name)
|
6607 | 6621 |
|
|
0 commit comments