Skip to content

Commit dbe3673

Browse files
committed
Replaced np.vectorize() with any() for minor performance improvement and add new test cases
1 parent 672575f commit dbe3673

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

pandas/core/algorithms.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -552,9 +552,8 @@ def isin(comps: ListLike, values: ListLike) -> npt.NDArray[np.bool_]:
552552
# Ensure values don't contain <NA>, otherwise it throws exception with np.in1d
553553
values_contains_NA = False
554554

555-
if values.size != 0:
556-
vectorized_check = np.vectorize(lambda v: v is NA)
557-
values_contains_NA = vectorized_check(values).any()
555+
if comps_array.dtype != object and len(values) <= 26:
556+
values_contains_NA = any(v is NA for v in values)
558557

559558
if (
560559
len(comps_array) > _MINIMUM_COMP_ARR_LEN

pandas/tests/series/methods/test_isin.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,9 @@ def test_isin_large_series_mixed_dtypes_and_nan(monkeypatch):
214214
@pytest.mark.parametrize("dtype, data, values, expected", [
215215
("boolean", [pd.NA, False, True], [False, pd.NA], [True, True, False]),
216216
("Int64", [pd.NA, 2, 1], [1, pd.NA], [True, False, True]),
217-
("Float64", [20.0, 30.0, pd.NA], [pd.NA], [False, False, True])
217+
("boolean", [pd.NA, False, True], [pd.NA, True, 'a', 20], [True, False, True]),
218+
("boolean", [pd.NA, False, True], [], [False, False, False]),
219+
("Float64", [20.0, 30.0, pd.NA], [pd.NA], [False, False, True]),
218220
])
219221
def test_isin_large_series_and_pdNA(dtype, data, values, expected, monkeypatch):
220222
# https://github.com/pandas-dev/pandas/issues/60678

0 commit comments

Comments
 (0)