diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index e42360558d284..9ebacabd7a839 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -291,6 +291,7 @@ Styler Other ^^^^^ - Bug in :meth:`CustomBusinessMonthBegin.__add__` (:meth:`CustomBusinessMonthEnd.__add__`) not applying the extra ``offset`` parameter when beginning (end) of the target month is already a business day (:issue:`41356`) +- Bug in :meth:`DataFrame.isin` and :meth:`Series.isin` incorrectly comparing ``None`` values to ``False`` (:issue:`35565`) .. ***DO NOT USE THIS SECTION*** diff --git a/pandas/_libs/ops.pyx b/pandas/_libs/ops.pyx index ac8a7f2cc57f7..c0ba5b0fcaedf 100644 --- a/pandas/_libs/ops.pyx +++ b/pandas/_libs/ops.pyx @@ -157,8 +157,9 @@ def vec_compare(ndarray[object] left, ndarray[object] right, object op) -> ndarr for i in range(n): x = left[i] y = right[i] - - if checknull(x) or checknull(y): + if x is None and y is None: + result[i] = False + elif checknull(x) or checknull(y): result[i] = True else: result[i] = PyObject_RichCompareBool(x, y, flag) @@ -166,8 +167,9 @@ def vec_compare(ndarray[object] left, ndarray[object] right, object op) -> ndarr for i in range(n): x = left[i] y = right[i] - - if checknull(x) or checknull(y): + if x is None and y is None: + result[i] = True + elif checknull(x) or checknull(y): result[i] = False else: result[i] = PyObject_RichCompareBool(x, y, flag) diff --git a/pandas/tests/test_algos.py b/pandas/tests/test_algos.py index b4836dffffa06..d7bd8ed9ef81a 100644 --- a/pandas/tests/test_algos.py +++ b/pandas/tests/test_algos.py @@ -1084,6 +1084,14 @@ def test_isin_float_df_string_search(self): expected_false = DataFrame({"values": [False, False]}) tm.assert_frame_equal(result, expected_false) + def test_isin_none(self): + # GH35565 + x = DataFrame([["foo", "bar"], [1, None]]) + y = x[1].copy() + res = x.isin(y) + expected = DataFrame([[False, True], [False, True]]) + tm.assert_frame_equal(res, expected) + class TestValueCounts: def test_value_counts(self):