Skip to content

BUG: Modify get_indexer_non_unique to account for nan keys #52655

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions pandas/_libs/index.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,8 @@ cdef class IndexEngine:
values = self.values
stargets = set(targets)

na_in_stargets = any([checknull(t) for t in stargets])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
na_in_stargets = any([checknull(t) for t in stargets])
na_in_stargets = any(checknull(t) for t in stargets)


n = len(values)
n_t = len(targets)
if n > 10_000:
Expand All @@ -374,7 +376,7 @@ cdef class IndexEngine:
if (
stargets and
len(stargets) < 5 and
not any([checknull(t) for t in stargets]) and
not na_in_stargets and
self.is_monotonic_increasing
):
# if there are few enough stargets and the index is monotonically
Expand All @@ -396,7 +398,7 @@ cdef class IndexEngine:
# otherwise, map by iterating through all items in the index

# short-circuit na check
if values.dtype == object:
if na_in_stargets:
check_na_values = True
# keep track of nas in values
found_nas = set()
Expand Down
26 changes: 26 additions & 0 deletions pandas/tests/indexes/base_class/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,32 @@ def test_get_indexer_non_unique_dtype_mismatch(self):
tm.assert_numpy_array_equal(np.array([-1], dtype=np.intp), indexes)
tm.assert_numpy_array_equal(np.array([0], dtype=np.intp), missing)

def test_get_indexer_non_unique_int_index(self):
indexes, missing = Index([np.nan, 100, 200, 100]).get_indexer_non_unique(
Index([np.nan])
)
tm.assert_numpy_array_equal(np.array([0], dtype=np.intp), indexes)
tm.assert_numpy_array_equal(np.array([], dtype=np.intp), missing)

indexes, missing = Index([np.nan, 100, 200, 100]).get_indexer_non_unique(
Index([np.nan, 100])
)
tm.assert_numpy_array_equal(np.array([0, 1, 3], dtype=np.intp), indexes)
tm.assert_numpy_array_equal(np.array([], dtype=np.intp), missing)

def test_get_indexer_non_unique_float_index(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you pytest.mark.parameterize this test with the one above?

indexes, missing = Index([np.nan, 100.0, 200.0, 100.0]).get_indexer_non_unique(
Index([np.nan])
)
tm.assert_numpy_array_equal(np.array([0], dtype=np.intp), indexes)
tm.assert_numpy_array_equal(np.array([], dtype=np.intp), missing)

indexes, missing = Index([np.nan, 100.0, 200.0, 100.0]).get_indexer_non_unique(
Index([np.nan, 100.0])
)
tm.assert_numpy_array_equal(np.array([0, 1, 3], dtype=np.intp), indexes)
tm.assert_numpy_array_equal(np.array([], dtype=np.intp), missing)


class TestGetLoc:
@pytest.mark.slow # to_flat_index takes a while
Expand Down