Skip to content

Commit 964a190

Browse files
Override RangeIndex._find_common_type_compat
1 parent e0b7c0a commit 964a190

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
@@ -6228,7 +6228,6 @@ def _maybe_promote(self, other: Index) -> tuple[Index, Index]:
62286228

62296229
return self, other
62306230

6231-
@final
62326231
def _find_common_type_compat(self, target) -> DtypeObj:
62336232
"""
62346233
Implementation of find_common_type that adjusts for Index-specific

pandas/core/indexes/range.py

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

30+
from pandas.core.dtypes.cast import (
31+
find_common_type,
32+
infer_dtype_from,
33+
)
3034
from pandas.core.dtypes.common import (
3135
ensure_platform_int,
3236
ensure_python_int,
@@ -51,12 +55,14 @@
5155
from pandas._typing import (
5256
Axis,
5357
Dtype,
58+
DtypeObj,
5459
NaPosition,
5560
Self,
5661
npt,
5762
)
5863
_empty_range = range(0)
5964
_dtype_int64 = np.dtype(np.int64)
65+
_dtype_obj = np.dtype("object")
6066

6167

6268
class RangeIndex(Index):
@@ -1084,3 +1090,21 @@ def take(
10841090

10851091
# _constructor so RangeIndex-> Index with an int64 dtype
10861092
return self._constructor._simple_new(taken, name=self.name)
1093+
1094+
def _find_common_type_compat(self, target) -> DtypeObj:
1095+
target_dtype, _ = infer_dtype_from(target)
1096+
1097+
# special case: if one dtype is uint64 and the other a signed int, return object
1098+
# See https://github.com/pandas-dev/pandas/issues/26778 for discussion
1099+
# Now it's:
1100+
# * float | [u]int -> float
1101+
# * uint64 | signed int -> object
1102+
# We may change union(float | [u]int) to go to object.
1103+
if self.dtype == "uint64" or target_dtype == "uint64":
1104+
if is_signed_integer_dtype(self.dtype) or is_signed_integer_dtype(
1105+
target_dtype
1106+
):
1107+
return _dtype_obj
1108+
1109+
dtype = find_common_type([self.dtype, target_dtype])
1110+
return dtype

0 commit comments

Comments
 (0)