Skip to content

Commit 1609e89

Browse files
more specific check for object/string combo
1 parent d202057 commit 1609e89

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

pandas/core/indexes/base.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6556,10 +6556,16 @@ def _maybe_cast_listlike_indexer(self, target) -> Index:
65566556
"""
65576557
Analogue to maybe_cast_indexer for get_indexer instead of get_loc.
65586558
"""
6559-
if not hasattr(target, "dtype") and self.dtype == object:
6560-
# Avoid inference for object since we are casting back later anyway
6561-
return Index(target, dtype=self.dtype)
6562-
return ensure_index(target)
6559+
target_index = ensure_index(target)
6560+
if (
6561+
not hasattr(target, "dtype")
6562+
and self.dtype == object
6563+
and target_index.dtype == "string"
6564+
):
6565+
# If we started with a list-like, avoid inference to string dtype if self
6566+
# is object dtype (coercing to string dtype will alter the missing values)
6567+
target_index = Index(target, dtype=self.dtype)
6568+
return target_index
65636569

65646570
@final
65656571
def _validate_indexer(

0 commit comments

Comments
 (0)