Skip to content

Commit 7958d6c

Browse files
Backport PR #56013 on branch 2.3.x (BUG: get_indexer rountripping through string dtype) (#60339)
Backport PR #56013: BUG: get_indexer rountripping through string dtype Co-authored-by: Patrick Hoefler <[email protected]>
1 parent 38565aa commit 7958d6c

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

doc/source/whatsnew/v2.3.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ Interval
119119

120120
Indexing
121121
^^^^^^^^
122-
-
122+
- Fixed bug in :meth:`Index.get_indexer` round-tripping through string dtype when ``infer_string`` is enabled (:issue:`55834`)
123123
-
124124

125125
Missing

pandas/core/indexes/base.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -6695,7 +6695,16 @@ def _maybe_cast_listlike_indexer(self, target) -> Index:
66956695
"""
66966696
Analogue to maybe_cast_indexer for get_indexer instead of get_loc.
66976697
"""
6698-
return ensure_index(target)
6698+
target_index = ensure_index(target)
6699+
if (
6700+
not hasattr(target, "dtype")
6701+
and self.dtype == object
6702+
and target_index.dtype == "string"
6703+
):
6704+
# If we started with a list-like, avoid inference to string dtype if self
6705+
# is object dtype (coercing to string dtype will alter the missing values)
6706+
target_index = Index(target, dtype=self.dtype)
6707+
return target_index
66996708

67006709
@final
67016710
def _validate_indexer(

pandas/tests/indexes/object/test_indexing.py

+9
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ def test_get_indexer_with_NA_values(
6262
expected = np.array([0, 1, -1], dtype=np.intp)
6363
tm.assert_numpy_array_equal(result, expected)
6464

65+
def test_get_indexer_infer_string_missing_values(self):
66+
# ensure the passed list is not cast to string but to object so that
67+
# the None value is matched in the index
68+
# https://github.com/pandas-dev/pandas/issues/55834
69+
idx = Index(["a", "b", None], dtype="object")
70+
result = idx.get_indexer([None, "x"])
71+
expected = np.array([2, -1], dtype=np.intp)
72+
tm.assert_numpy_array_equal(result, expected)
73+
6574

6675
class TestGetIndexerNonUnique:
6776
def test_get_indexer_non_unique_nas(self, nulls_fixture):

0 commit comments

Comments
 (0)