Skip to content

Commit d5c7e9a

Browse files
committed
BUG: get_indexer rountripping through string dtype
1 parent e5301a8 commit d5c7e9a

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

doc/source/whatsnew/v2.1.4.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Fixed regressions
2222
Bug fixes
2323
~~~~~~~~~
2424
- Bug in :class:`Series` constructor raising DeprecationWarning when ``index`` is a list of :class:`Series` (:issue:`55228`)
25+
- Fixed bug in :meth:`Index.get_indexer` round-tripping through string dtype when ``infer_string`` is enabled (:issue:`55834`)
2526
-
2627

2728
.. ---------------------------------------------------------------------------

pandas/core/indexes/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6580,6 +6580,9 @@ def _maybe_cast_listlike_indexer(self, target) -> Index:
65806580
"""
65816581
Analogue to maybe_cast_indexer for get_indexer instead of get_loc.
65826582
"""
6583+
if not hasattr(target, "dtype") and self.dtype == object:
6584+
# Avoid inference for object since we are casting back later anyway
6585+
return Index(target, dtype=self.dtype)
65836586
return ensure_index(target)
65846587

65856588
@final

pandas/tests/indexes/object/test_indexing.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55

66
from pandas._libs.missing import is_matching_na
7+
import pandas.util._test_decorators as td
78

89
import pandas as pd
910
from pandas import Index
@@ -55,6 +56,14 @@ def test_get_indexer_with_NA_values(
5556
expected = np.array([0, 1, -1], dtype=np.intp)
5657
tm.assert_numpy_array_equal(result, expected)
5758

59+
@td.skip_if_no("pyarrow")
60+
def test_get_indexer_infer_string_missing_values(self):
61+
# GH#55834
62+
idx = Index(["a", "b", None], dtype="object")
63+
result = idx.get_indexer([None, "x"])
64+
expected = np.array([2, -1])
65+
tm.assert_numpy_array_equal(result, expected)
66+
5867

5968
class TestGetIndexerNonUnique:
6069
def test_get_indexer_non_unique_nas(self, nulls_fixture):

0 commit comments

Comments
 (0)