Skip to content

PERF: avoid unnecessary casting in merge #53771

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 14 additions & 28 deletions pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from pandas._typing import (
AnyArrayLike,
ArrayLike,
DtypeObj,
IndexLabel,
JoinHow,
MergeHow,
Expand All @@ -48,7 +47,6 @@
from pandas.core.dtypes.base import ExtensionDtype
from pandas.core.dtypes.cast import find_common_type
from pandas.core.dtypes.common import (
ensure_float64,
ensure_int64,
ensure_object,
is_bool,
Expand Down Expand Up @@ -1843,23 +1841,6 @@ def _asof_by_function(direction: str):
return getattr(libjoin, name, None)


_type_casters = {
"int64_t": ensure_int64,
"double": ensure_float64,
"object": ensure_object,
}


def _get_cython_type_upcast(dtype: DtypeObj) -> str:
"""Upcast a dtype to 'int64_t', 'double', or 'object'"""
if is_integer_dtype(dtype):
return "int64_t"
elif is_float_dtype(dtype):
return "double"
else:
return "object"


class _AsOfMerge(_OrderedMerge):
_merge_type = "asof_merge"

Expand Down Expand Up @@ -2161,23 +2142,28 @@ def injection(obj: ArrayLike):
if len(left_by_values) == 1:
lbv = left_by_values[0]
rbv = right_by_values[0]

# TODO: conversions for EAs that can be no-copy.
lbv = np.asarray(lbv)
rbv = np.asarray(rbv)
else:
# We get here with non-ndarrays in test_merge_by_col_tz_aware
# and test_merge_groupby_multiple_column_with_categorical_column
lbv = flip(left_by_values)
rbv = flip(right_by_values)
lbv = ensure_object(lbv)
rbv = ensure_object(rbv)

# upcast 'by' parameter because HashTable is limited
by_type = _get_cython_type_upcast(lbv.dtype)
by_type_caster = _type_casters[by_type]
# error: Incompatible types in assignment (expression has type
# "ndarray[Any, dtype[generic]]", variable has type
# "List[Union[Union[ExtensionArray, ndarray[Any, Any]], Index, Series]]")
left_by_values = by_type_caster(lbv) # type: ignore[assignment]
# "Union[ndarray[Any, dtype[Any]], ndarray[Any, dtype[object_]]]",
# variable has type "List[Union[Union[ExtensionArray,
# ndarray[Any, Any]], Index, Series]]")
right_by_values = rbv # type: ignore[assignment]
# error: Incompatible types in assignment (expression has type
# "ndarray[Any, dtype[generic]]", variable has type
# "List[Union[Union[ExtensionArray, ndarray[Any, Any]], Index, Series]]")
right_by_values = by_type_caster(rbv) # type: ignore[assignment]
# "Union[ndarray[Any, dtype[Any]], ndarray[Any, dtype[object_]]]",
# variable has type "List[Union[Union[ExtensionArray,
# ndarray[Any, Any]], Index, Series]]")
left_by_values = lbv # type: ignore[assignment]

# choose appropriate function by type
func = _asof_by_function(self.direction)
Expand Down