Skip to content

Commit d633abd

Browse files
authored
BUG: index.getitem raising ValueError with boolean indexer and NA (#45985)
1 parent 28d12e7 commit d633abd

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

doc/source/whatsnew/v1.5.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ Indexing
326326
- Bug in :meth:`loc.__setitem__` treating ``range`` keys as positional instead of label-based (:issue:`45479`)
327327
- Bug in :meth:`Series.__setitem__` when setting ``boolean`` dtype values containing ``NA`` incorrectly raising instead of casting to ``boolean`` dtype (:issue:`45462`)
328328
- Bug in :meth:`Series.__setitem__` where setting :attr:`NA` into a numeric-dtpye :class:`Series` would incorrectly upcast to object-dtype rather than treating the value as ``np.nan`` (:issue:`44199`)
329+
- Bug in :meth:`Index.__getitem__` raising ``ValueError`` when indexer is from boolean dtype with ``NA`` (:issue:`45806`)
329330
- Bug in :meth:`Series.mask` with ``inplace=True`` or setting values with a boolean mask with small integer dtypes incorrectly raising (:issue:`45750`)
330331
- Bug in :meth:`DataFrame.mask` with ``inplace=True`` and ``ExtensionDtype`` columns incorrectly raising (:issue:`45577`)
331332
- Bug in getting a column from a DataFrame with an object-dtype row index with datetime-like values: the resulting Series now preserves the exact object-dtype Index from the parent DataFrame (:issue:`42950`)

pandas/core/indexes/base.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5226,7 +5226,10 @@ def __getitem__(self, key):
52265226
# takes 166 µs + 2.1 ms and cuts the ndarray.__getitem__
52275227
# time below from 3.8 ms to 496 µs
52285228
# if we already have ndarray[bool], the overhead is 1.4 µs or .25%
5229-
key = np.asarray(key, dtype=bool)
5229+
if is_extension_array_dtype(getattr(key, "dtype", None)):
5230+
key = key.to_numpy(dtype=bool, na_value=False)
5231+
else:
5232+
key = np.asarray(key, dtype=bool)
52305233

52315234
result = getitem(key)
52325235
# Because we ruled out integer above, we always get an arraylike here

pandas/tests/indexes/base_class/test_indexing.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,11 @@ def test_get_loc_nan_object_dtype_nonmonotonic_nonunique(self):
7676
# we don't match at all on mismatched NA
7777
with pytest.raises(KeyError, match="NaT"):
7878
idx.get_loc(NaT)
79+
80+
81+
def test_getitem_boolean_ea_indexer():
82+
# GH#45806
83+
ser = pd.Series([True, False, pd.NA], dtype="boolean")
84+
result = ser.index[ser]
85+
expected = Index([0])
86+
tm.assert_index_equal(result, expected)

0 commit comments

Comments
 (0)