Skip to content

Commit

Permalink
Backport PR #54694 on branch 2.1.x (MAINT: Remove np.in1d function …
Browse files Browse the repository at this point in the history
…calls) (#54697)

Backport PR #54694: MAINT: Remove `np.in1d` function calls

Co-authored-by: Mateusz Sokół <8431159+mtsokol@users.noreply.github.com>
  • Loading branch information
meeseeksmachine and mtsokol committed Aug 23, 2023
1 parent 1dbd792 commit 503cef4
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
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

0 comments on commit 503cef4

Please sign in to comment.