Skip to content

Commit 46af335

Browse files
committed
ENH: Cython ismember function for Series.isin, really fast
1 parent 049b71d commit 46af335

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

pandas/core/series.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1628,7 +1628,9 @@ def isin(self, values):
16281628
isin : Series (boolean dtype)
16291629
"""
16301630
value_set = set(values)
1631-
return self.map(value_set.__contains__)
1631+
result = lib.ismember(self, value_set)
1632+
# return self.map(value_set.__contains__)
1633+
return Series(result, self.index, name=self.name)
16321634

16331635
#-------------------------------------------------------------------------------
16341636
# Miscellaneous

pandas/src/tseries.pyx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,26 @@ def isAllDates(ndarray[object, ndim=1] arr):
164164

165165
return True
166166

167+
def ismember(ndarray arr, set values):
168+
cdef:
169+
Py_ssize_t i, n
170+
flatiter it
171+
ndarray[uint8_t] result
172+
object val
173+
174+
it = <flatiter> PyArray_IterNew(arr)
175+
n = len(arr)
176+
result = np.empty(n, dtype=np.uint8)
177+
for i from 0 <= i < n:
178+
val = PyArray_GETITEM(arr, PyArray_ITER_DATA(it))
179+
if val in values:
180+
result[i] = 1
181+
else:
182+
result[i] = 0
183+
PyArray_ITER_NEXT(it)
184+
185+
return result.view(np.bool_)
186+
167187
#----------------------------------------------------------------------
168188
# datetime / io related
169189

0 commit comments

Comments
 (0)