Skip to content

Commit 52f8131

Browse files
committed
Remove flgs and retain try-except
1 parent dd37f9c commit 52f8131

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

pandas/core/algorithms.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -427,37 +427,40 @@ def isin(comps, values):
427427
values, _, _ = _ensure_data(values)
428428

429429
# faster for larger cases to use np.in1d
430-
f = lambda x, y: htable.ismember_object(x.astype(object), y.astype(object))
430+
f = lambda x, y: htable.ismember_object(x, y)
431431

432432
comps_types = set(type(v) for v in comps)
433433
values_types = set(type(v) for v in values)
434434

435-
is_int = lambda x: ((x == np.int64) or (x == int))
436-
is_float = lambda x: ((x == np.float64) or (x == float))
437-
438-
int_flg = False
439-
float_flg = False
440-
441435
if len(comps_types) == len(values_types) == 1:
442436
comps_types = comps_types.pop()
443437
values_types = values_types.pop()
444-
int_flg = (is_int(comps_types) and is_int(values_types))
445-
float_flg = (is_float(comps_types) and is_float(values_types))
438+
439+
is_int = lambda x: ((x == np.int64) or (x == int))
440+
is_float = lambda x: ((x == np.float64) or (x == float))
446441

447442
# GH16012
448443
# Ensure np.in1d doesn't get object types or it *may* throw an exception
449444
if len(comps) > 1000000 and not is_object_dtype(comps):
450445
f = lambda x, y: np.in1d(x, y)
451-
elif int_flg:
452-
values = values.astype('int64', copy=False)
453-
comps = comps.astype('int64', copy=False)
454-
f = lambda x, y: htable.ismember_int64(x, y)
455-
456-
elif float_flg:
457-
values = values.astype('float64', copy=False)
458-
comps = comps.astype('float64', copy=False)
459-
checknull = isna(values).any()
460-
f = lambda x, y: htable.ismember_float64(x, y, checknull)
446+
elif (is_int(comps_types) and is_int(values_types)):
447+
try:
448+
values = values.astype('int64', copy=False)
449+
comps = comps.astype('int64', copy=False)
450+
f = lambda x, y: htable.ismember_int64(x, y)
451+
except (TypeError, ValueError):
452+
values = values.astype(object)
453+
comps = comps.astype(object)
454+
455+
elif (is_float(comps_types) and is_float(values_types)):
456+
try:
457+
values = values.astype('float64', copy=False)
458+
comps = comps.astype('float64', copy=False)
459+
checknull = isna(values).any()
460+
f = lambda x, y: htable.ismember_float64(x, y, checknull)
461+
except (TypeError, ValueError):
462+
values = values.astype(object)
463+
comps = comps.astype(object)
461464

462465
return f(comps, values)
463466

0 commit comments

Comments
 (0)