Skip to content

Commit a8a013a

Browse files
Override RangeIndex._find_common_type_compat
1 parent d158439 commit a8a013a

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

pandas/core/indexes/base.py

-1
Original file line numberDiff line numberDiff line change
@@ -6273,7 +6273,6 @@ def _maybe_promote(self, other: Index) -> tuple[Index, Index]:
62736273

62746274
return self, other
62756275

6276-
@final
62776276
def _find_common_type_compat(self, target) -> DtypeObj:
62786277
"""
62796278
Implementation of find_common_type that adjusts for Index-specific

pandas/core/indexes/range.py

+24
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
doc,
2929
)
3030

31+
from pandas.core.dtypes.cast import (
32+
find_common_type,
33+
infer_dtype_from,
34+
)
3135
from pandas.core.dtypes.common import (
3236
ensure_platform_int,
3337
ensure_python_int,
@@ -52,12 +56,14 @@
5256
from pandas._typing import (
5357
Axis,
5458
Dtype,
59+
DtypeObj,
5560
NaPosition,
5661
Self,
5762
npt,
5863
)
5964
_empty_range = range(0)
6065
_dtype_int64 = np.dtype(np.int64)
66+
_dtype_obj = np.dtype("object")
6167

6268

6369
class RangeIndex(Index):
@@ -1152,3 +1158,21 @@ def take(
11521158

11531159
# _constructor so RangeIndex-> Index with an int64 dtype
11541160
return self._constructor._simple_new(taken, name=self.name)
1161+
1162+
def _find_common_type_compat(self, target) -> DtypeObj:
1163+
target_dtype, _ = infer_dtype_from(target)
1164+
1165+
# special case: if one dtype is uint64 and the other a signed int, return object
1166+
# See https://github.com/pandas-dev/pandas/issues/26778 for discussion
1167+
# Now it's:
1168+
# * float | [u]int -> float
1169+
# * uint64 | signed int -> object
1170+
# We may change union(float | [u]int) to go to object.
1171+
if self.dtype == "uint64" or target_dtype == "uint64":
1172+
if is_signed_integer_dtype(self.dtype) or is_signed_integer_dtype(
1173+
target_dtype
1174+
):
1175+
return _dtype_obj
1176+
1177+
dtype = find_common_type([self.dtype, target_dtype])
1178+
return dtype

0 commit comments

Comments
 (0)