From 750f27b0a919365ebe3906dcd0102a4304e5c6ff Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Wed, 7 Sep 2022 09:12:36 +0200 Subject: [PATCH] Backport PR #48411: REGR: get_loc for ExtensionEngine not returning bool indexer for na --- pandas/_libs/index.pyx | 2 +- pandas/tests/indexes/test_indexing.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/pandas/_libs/index.pyx b/pandas/_libs/index.pyx index 0cf7c4d45c634..617760c2981c4 100644 --- a/pandas/_libs/index.pyx +++ b/pandas/_libs/index.pyx @@ -1061,7 +1061,7 @@ cdef class ExtensionEngine(SharedEngine): cdef ndarray _get_bool_indexer(self, val): if checknull(val): - return self.values.isna().view("uint8") + return self.values.isna() try: return self.values == val diff --git a/pandas/tests/indexes/test_indexing.py b/pandas/tests/indexes/test_indexing.py index 739039241a31d..ab934df992d61 100644 --- a/pandas/tests/indexes/test_indexing.py +++ b/pandas/tests/indexes/test_indexing.py @@ -20,6 +20,7 @@ from pandas.errors import InvalidIndexError from pandas import ( + NA, DatetimeIndex, Index, IntervalIndex, @@ -221,6 +222,13 @@ def test_get_loc_generator(self, index): # MultiIndex specifically checks for generator; others for scalar index.get_loc(x for x in range(5)) + def test_get_loc_masked_duplicated_na(self): + # GH#48411 + idx = Index([1, 2, NA, NA], dtype="Int64") + result = idx.get_loc(NA) + expected = np.array([False, False, True, True]) + tm.assert_numpy_array_equal(result, expected) + class TestGetIndexer: def test_get_indexer_base(self, index): @@ -253,6 +261,13 @@ def test_get_indexer_consistency(self, index): assert isinstance(indexer, np.ndarray) assert indexer.dtype == np.intp + def test_get_indexer_masked_duplicated_na(self): + # GH#48411 + idx = Index([1, 2, NA, NA], dtype="Int64") + result = idx.get_indexer_for(Index([1, NA], dtype="Int64")) + expected = np.array([0, 2, 3], dtype=result.dtype) + tm.assert_numpy_array_equal(result, expected) + class TestConvertSliceIndexer: def test_convert_almost_null_slice(self, index):