Skip to content
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

Backport PR #54694 on branch 2.1.x (MAINT: Remove np.in1d function calls) #54697

Merged
Merged
Show file tree
Hide file tree
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
Backport PR #54694: MAINT: Remove np.in1d function calls
  • Loading branch information
mtsokol authored and meeseeksmachine committed Aug 22, 2023
commit 6299599997b75f4316ad1bba81ac0d7fafa5b4e5
8 changes: 4 additions & 4 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,9 +518,9 @@ def isin(comps: ListLike, values: ListLike) -> npt.NDArray[np.bool_]:
return isin(np.asarray(comps_array), np.asarray(values))

# GH16012
# Ensure np.in1d doesn't get object types or it *may* throw an exception
# Ensure np.isin doesn't get object types or it *may* throw an exception
# Albeit hashmap has O(1) look-up (vs. O(logn) in sorted array),
# in1d is faster for small sizes
# isin is faster for small sizes
if (
len(comps_array) > _MINIMUM_COMP_ARR_LEN
and len(values) <= 26
Expand All @@ -531,10 +531,10 @@ def isin(comps: ListLike, values: ListLike) -> npt.NDArray[np.bool_]:
if isna(values).any():

def f(c, v):
return np.logical_or(np.in1d(c, v), np.isnan(c))
return np.logical_or(np.isin(c, v).ravel(), np.isnan(c))

else:
f = np.in1d
f = lambda a, b: np.isin(a, b).ravel()

else:
common = np_find_common_type(values.dtype, comps_array.dtype)
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1844,14 +1844,14 @@ def isin(self, values) -> npt.NDArray[np.bool_]:
# complex128 ndarray is much more performant.
left = self._combined.view("complex128")
right = values._combined.view("complex128")
# error: Argument 1 to "in1d" has incompatible type
# error: Argument 1 to "isin" has incompatible type
# "Union[ExtensionArray, ndarray[Any, Any],
# ndarray[Any, dtype[Any]]]"; expected
# "Union[_SupportsArray[dtype[Any]],
# _NestedSequence[_SupportsArray[dtype[Any]]], bool,
# int, float, complex, str, bytes, _NestedSequence[
# Union[bool, int, float, complex, str, bytes]]]"
return np.in1d(left, right) # type: ignore[arg-type]
return np.isin(left, right).ravel() # type: ignore[arg-type]

elif needs_i8_conversion(self.left.dtype) ^ needs_i8_conversion(
values.left.dtype
Expand Down
4 changes: 2 additions & 2 deletions pandas/tseries/holiday.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,11 @@ def dates(
holiday_dates = self._apply_rule(dates)
if self.days_of_week is not None:
holiday_dates = holiday_dates[
np.in1d(
np.isin(
# error: "DatetimeIndex" has no attribute "dayofweek"
holiday_dates.dayofweek, # type: ignore[attr-defined]
self.days_of_week,
)
).ravel()
]

if self.start_date is not None:
Expand Down