Skip to content

Commit fed199c

Browse files
committed
Move uint64 check to _find_common_type_compat
1 parent 47098f2 commit fed199c

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

pandas/core/dtypes/cast.py

+1-10
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868
is_numeric_dtype,
6969
is_object_dtype,
7070
is_scalar,
71-
is_signed_integer_dtype,
7271
is_string_dtype,
7372
is_timedelta64_dtype,
7473
is_unsigned_integer_dtype,
@@ -1780,14 +1779,13 @@ def ensure_nanosecond_dtype(dtype: DtypeObj) -> DtypeObj:
17801779
return dtype
17811780

17821781

1783-
def find_common_type(types: list[DtypeObj], *, strict_uint64: bool = False) -> DtypeObj:
1782+
def find_common_type(types: list[DtypeObj]) -> DtypeObj:
17841783
"""
17851784
Find a common data type among the given dtypes.
17861785
17871786
Parameters
17881787
----------
17891788
types : list of dtypes
1790-
strict_uint64 : if True, object dtype is returned if uint64 and signed int present.
17911789
17921790
Returns
17931791
-------
@@ -1833,13 +1831,6 @@ def find_common_type(types: list[DtypeObj], *, strict_uint64: bool = False) -> D
18331831
if is_integer_dtype(t) or is_float_dtype(t) or is_complex_dtype(t):
18341832
return np.dtype("object")
18351833

1836-
# Index.union is special: uint64 & signed int -> object
1837-
if strict_uint64:
1838-
has_uint64 = any(t == "uint64" for t in types)
1839-
has_signed_int = any(is_signed_integer_dtype(t) for t in types)
1840-
if has_uint64 and has_signed_int:
1841-
return np.dtype("object")
1842-
18431834
# error: Argument 1 to "find_common_type" has incompatible type
18441835
# "List[Union[dtype, ExtensionDtype]]"; expected "Sequence[Union[dtype,
18451836
# None, type, _SupportsDtype, str, Tuple[Any, int], Tuple[Any, Union[int,

pandas/core/indexes/base.py

+14-8
Original file line numberDiff line numberDiff line change
@@ -2961,14 +2961,7 @@ def union(self, other, sort=None):
29612961
stacklevel=2,
29622962
)
29632963

2964-
dtype = find_common_type([self.dtype, other.dtype], strict_uint64=True)
2965-
# Right now, we treat union(float, [u]int) a bit special.
2966-
# See https://github.com/pandas-dev/pandas/issues/26778 for discussion
2967-
# Now it's:
2968-
# * float | [u]int -> float
2969-
# * uint64 | signed int -> object
2970-
# We may change union(float | [u]int) to go to object.
2971-
2964+
dtype = self._find_common_type_compat(other)
29722965
left = self.astype(dtype, copy=False)
29732966
right = other.astype(dtype, copy=False)
29742967
return left.union(right, sort=sort)
@@ -5402,6 +5395,19 @@ def _find_common_type_compat(self, target) -> DtypeObj:
54025395
return IntervalDtype(np.float64, closed=self.closed)
54035396

54045397
target_dtype, _ = infer_dtype_from(target, pandas_dtype=True)
5398+
5399+
# special case: if either is uint64 and other dtype is signed int, return object
5400+
# See https://github.com/pandas-dev/pandas/issues/26778 for discussion
5401+
# Now it's:
5402+
# * float | [u]int -> float
5403+
# * uint64 | signed int -> object
5404+
# We may change union(float | [u]int) to go to object.
5405+
if self.dtype == "uint64" or target_dtype == "uint64":
5406+
if is_signed_integer_dtype(self.dtype) or is_signed_integer_dtype(
5407+
target_dtype
5408+
):
5409+
return np.dtype("object")
5410+
54055411
dtype = find_common_type([self.dtype, target_dtype])
54065412
if dtype.kind in ["i", "u"]:
54075413
# TODO: what about reversed with self being categorical?

0 commit comments

Comments
 (0)