Skip to content
This repository was archived by the owner on Jul 10, 2024. It is now read-only.

Commit d621e8e

Browse files
PERF: improve find_common_type perf for special case of all-equal dtypes (pandas-dev#44594)
1 parent 0e9f929 commit d621e8e

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

pandas/_libs/lib.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,4 @@ def get_reverse_indexer(
228228
length: int,
229229
) -> npt.NDArray[np.intp]: ...
230230
def is_bool_list(obj: list) -> bool: ...
231+
def dtypes_all_equal(types: list[DtypeObj]) -> bool: ...

pandas/_libs/lib.pyx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3038,3 +3038,25 @@ def is_bool_list(obj: list) -> bool:
30383038

30393039
# Note: we return True for empty list
30403040
return True
3041+
3042+
3043+
def dtypes_all_equal(list types not None) -> bool:
3044+
"""
3045+
Faster version for:
3046+
3047+
first = types[0]
3048+
all(is_dtype_equal(first, t) for t in types[1:])
3049+
3050+
And assuming all elements in the list are np.dtype/ExtensionDtype objects
3051+
3052+
See timings at https://github.com/pandas-dev/pandas/pull/44594
3053+
"""
3054+
first = types[0]
3055+
for t in types[1:]:
3056+
try:
3057+
if not t == first:
3058+
return False
3059+
except (TypeError, AttributeError):
3060+
return False
3061+
else:
3062+
return True

pandas/core/dtypes/cast.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1815,7 +1815,7 @@ def find_common_type(types: list[DtypeObj]) -> DtypeObj:
18151815

18161816
# workaround for find_common_type([np.dtype('datetime64[ns]')] * 2)
18171817
# => object
1818-
if all(is_dtype_equal(first, t) for t in types[1:]):
1818+
if lib.dtypes_all_equal(list(types)):
18191819
return first
18201820

18211821
# get unique types (dict.fromkeys is used as order-preserving set())

0 commit comments

Comments
 (0)