Skip to content

Commit b0f1373

Browse files
committed
Fixed #52234
* Copied fix from https://github.com/ltauro/pandas/tree/nan-not-in-index and applied review comments
1 parent 33199e1 commit b0f1373

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ Indexing
422422
^^^^^^^^
423423
- Bug in :meth:`DataFrame.__setitem__` losing dtype when setting a :class:`DataFrame` into duplicated columns (:issue:`53143`)
424424
- Bug in :meth:`DataFrame.__setitem__` with a boolean mask and :meth:`DataFrame.putmask` with mixed non-numeric dtypes and a value other than ``NaN`` incorrectly raising ``TypeError`` (:issue:`53291`)
425+
- Bug when using ``nan`` to index (:issue:`52234`)
425426

426427
Missing
427428
^^^^^^^

pandas/_libs/index.pyx

+4-2
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,8 @@ cdef class IndexEngine:
360360
values = self.values
361361
stargets = set(targets)
362362

363+
na_in_stargets = any(checknull(t) for t in stargets)
364+
363365
n = len(values)
364366
n_t = len(targets)
365367
if n > 10_000:
@@ -374,7 +376,7 @@ cdef class IndexEngine:
374376
if (
375377
stargets and
376378
len(stargets) < 5 and
377-
not any([checknull(t) for t in stargets]) and
379+
not na_in_stargets and
378380
self.is_monotonic_increasing
379381
):
380382
# if there are few enough stargets and the index is monotonically
@@ -396,7 +398,7 @@ cdef class IndexEngine:
396398
# otherwise, map by iterating through all items in the index
397399

398400
# short-circuit na check
399-
if values.dtype == object:
401+
if na_in_stargets:
400402
check_na_values = True
401403
# keep track of nas in values
402404
found_nas = set()

pandas/tests/indexes/base_class/test_indexing.py

+18
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,24 @@ def test_get_indexer_non_unique_dtype_mismatch(self):
3737
tm.assert_numpy_array_equal(np.array([-1], dtype=np.intp), indexes)
3838
tm.assert_numpy_array_equal(np.array([0], dtype=np.intp), missing)
3939

40+
@pytest.mark.parametrize(
41+
"idx_values,idx_non_unique",
42+
[
43+
([np.nan, 100, 200, 100], [np.nan, 100]),
44+
([np.nan, 100.0, 200.0, 100.0], [np.nan, 100.0]),
45+
],
46+
)
47+
def test_get_indexer_non_unique_int_index(self, idx_values, idx_non_unique):
48+
indexes, missing = Index(idx_values).get_indexer_non_unique(Index([np.nan]))
49+
tm.assert_numpy_array_equal(np.array([0], dtype=np.intp), indexes)
50+
tm.assert_numpy_array_equal(np.array([], dtype=np.intp), missing)
51+
52+
indexes, missing = Index(idx_values).get_indexer_non_unique(
53+
Index(idx_non_unique)
54+
)
55+
tm.assert_numpy_array_equal(np.array([0, 1, 3], dtype=np.intp), indexes)
56+
tm.assert_numpy_array_equal(np.array([], dtype=np.intp), missing)
57+
4058

4159
class TestGetLoc:
4260
@pytest.mark.slow # to_flat_index takes a while

0 commit comments

Comments
 (0)