Skip to content
Merged
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,7 @@ Indexing
- Bug in reindexing of :class:`DataFrame` with :class:`PeriodDtype` columns in case of consolidated block (:issue:`60980`, :issue:`60273`)
- Bug in :meth:`DataFrame.loc.__getitem__` and :meth:`DataFrame.iloc.__getitem__` with a :class:`CategoricalDtype` column with integer categories raising when trying to index a row containing a ``NaN`` entry (:issue:`58954`)
- Bug in :meth:`Index.__getitem__` incorrectly raising with a 0-dim ``np.ndarray`` key (:issue:`55601`)
- Bug in :meth:`Index.get_indexer` not casting missing values correctly for new string datatype (:issue:`55833`)
- Bug in adding new rows with :meth:`DataFrame.loc.__setitem__` or :class:`Series.loc.__setitem__` which failed to retain dtype on the object's index in some cases (:issue:`41626`)
- Bug in indexing on a :class:`DatetimeIndex` with a ``timestamp[pyarrow]`` dtype or on a :class:`TimedeltaIndex` with a ``duration[pyarrow]`` dtype (:issue:`62277`)

Expand Down
8 changes: 8 additions & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6619,6 +6619,14 @@ def _maybe_cast_listlike_indexer(self, target) -> Index:
# If we started with a list-like, avoid inference to string dtype if self
# is object dtype (coercing to string dtype will alter the missing values)
target_index = Index(target, dtype=self.dtype)
elif (
not hasattr(target, "dtype")
and isinstance(self.dtype, StringDtype)
and self.dtype.na_value is np.nan
and using_string_dtype()
):
# Fill missing values to ensure consistent missing value representation
target_index = target_index.fillna(np.nan)
return target_index

@final
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/indexes/ranges/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ def test_get_indexer_decreasing(self, stop):
expected = np.array([-1, 2, -1, -1, 1, -1, -1, 0, -1], dtype=np.intp)
tm.assert_numpy_array_equal(result, expected)

def test_get_indexer_missing_value_casting_string_dtype(self):
# GH#55833
idx = Index(["a", "b", None])
result = idx.get_indexer([None])
expected = np.array([2], dtype=np.intp)
tm.assert_numpy_array_equal(result, expected)

result = idx.get_indexer([None, True])
expected = np.array([2, -1], dtype=np.intp)
tm.assert_numpy_array_equal(result, expected)


class TestTake:
def test_take_preserve_name(self):
Expand Down
Loading