Skip to content

Fix .isin Considers "1" and 1 equal #34267

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

Merged
merged 11 commits into from
Jun 8, 2020
9 changes: 8 additions & 1 deletion pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,14 @@ def isin(comps: AnyArrayLike, values: AnyArrayLike) -> np.ndarray:
# handle categoricals
return comps.isin(values) # type: ignore

comps, dtype = _ensure_data(comps)
if hasattr(comps, "dtype") and hasattr(comps.dtype, "name"):
if comps.dtype.name == "int64":
comps, dtype = _ensure_data(comps, dtype=object)
else:
comps, dtype = _ensure_data(comps)
else:
comps, dtype = _ensure_data(comps)

values, _ = _ensure_data(values, dtype=dtype)

# faster for larger cases to use np.in1d
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/frame/methods/test_isin.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,16 @@ def test_isin_empty_datetimelike(self):
tm.assert_frame_equal(result, expected)
result = df1_td.isin(df3)
tm.assert_frame_equal(result, expected)

def test_isin_int_df_string_search(self):
"""Comparing df with int`s (1,2) with a string at isin() ("1")
-> should not match values because int 1 is not equal str 1"""
df = DataFrame({"values": [1, 2]})
result = df.isin(["1"])
expected_false = pd.DataFrame({"values": [False, False]})
tm.assert_frame_equal(result, expected_false)

# Comparing df with int`s with a int at isin() -> should be fine
result = df.isin([1])
expected_true = DataFrame({"values": [True, False]})
tm.assert_frame_equal(result, expected_true)