Skip to content

Commit 7133cf8

Browse files
authored
PERF: avoid unnecessary casting in merge (#53771)
1 parent 996e210 commit 7133cf8

File tree

1 file changed

+14
-28
lines changed

1 file changed

+14
-28
lines changed

pandas/core/reshape/merge.py

+14-28
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
from pandas._typing import (
3030
AnyArrayLike,
3131
ArrayLike,
32-
DtypeObj,
3332
IndexLabel,
3433
JoinHow,
3534
MergeHow,
@@ -48,7 +47,6 @@
4847
from pandas.core.dtypes.base import ExtensionDtype
4948
from pandas.core.dtypes.cast import find_common_type
5049
from pandas.core.dtypes.common import (
51-
ensure_float64,
5250
ensure_int64,
5351
ensure_object,
5452
is_bool,
@@ -1860,23 +1858,6 @@ def _asof_by_function(direction: str):
18601858
return getattr(libjoin, name, None)
18611859

18621860

1863-
_type_casters = {
1864-
"int64_t": ensure_int64,
1865-
"double": ensure_float64,
1866-
"object": ensure_object,
1867-
}
1868-
1869-
1870-
def _get_cython_type_upcast(dtype: DtypeObj) -> str:
1871-
"""Upcast a dtype to 'int64_t', 'double', or 'object'"""
1872-
if is_integer_dtype(dtype):
1873-
return "int64_t"
1874-
elif is_float_dtype(dtype):
1875-
return "double"
1876-
else:
1877-
return "object"
1878-
1879-
18801861
class _AsOfMerge(_OrderedMerge):
18811862
_merge_type = "asof_merge"
18821863

@@ -2176,23 +2157,28 @@ def injection(obj: ArrayLike):
21762157
if len(left_by_values) == 1:
21772158
lbv = left_by_values[0]
21782159
rbv = right_by_values[0]
2160+
2161+
# TODO: conversions for EAs that can be no-copy.
2162+
lbv = np.asarray(lbv)
2163+
rbv = np.asarray(rbv)
21792164
else:
21802165
# We get here with non-ndarrays in test_merge_by_col_tz_aware
21812166
# and test_merge_groupby_multiple_column_with_categorical_column
21822167
lbv = flip(left_by_values)
21832168
rbv = flip(right_by_values)
2169+
lbv = ensure_object(lbv)
2170+
rbv = ensure_object(rbv)
21842171

2185-
# upcast 'by' parameter because HashTable is limited
2186-
by_type = _get_cython_type_upcast(lbv.dtype)
2187-
by_type_caster = _type_casters[by_type]
21882172
# error: Incompatible types in assignment (expression has type
2189-
# "ndarray[Any, dtype[generic]]", variable has type
2190-
# "List[Union[Union[ExtensionArray, ndarray[Any, Any]], Index, Series]]")
2191-
left_by_values = by_type_caster(lbv) # type: ignore[assignment]
2173+
# "Union[ndarray[Any, dtype[Any]], ndarray[Any, dtype[object_]]]",
2174+
# variable has type "List[Union[Union[ExtensionArray,
2175+
# ndarray[Any, Any]], Index, Series]]")
2176+
right_by_values = rbv # type: ignore[assignment]
21922177
# error: Incompatible types in assignment (expression has type
2193-
# "ndarray[Any, dtype[generic]]", variable has type
2194-
# "List[Union[Union[ExtensionArray, ndarray[Any, Any]], Index, Series]]")
2195-
right_by_values = by_type_caster(rbv) # type: ignore[assignment]
2178+
# "Union[ndarray[Any, dtype[Any]], ndarray[Any, dtype[object_]]]",
2179+
# variable has type "List[Union[Union[ExtensionArray,
2180+
# ndarray[Any, Any]], Index, Series]]")
2181+
left_by_values = lbv # type: ignore[assignment]
21962182

21972183
# choose appropriate function by type
21982184
func = _asof_by_function(self.direction)

0 commit comments

Comments
 (0)