Skip to content

Commit f2a15c7

Browse files
committed
BUG: Index.__getitem__ with 0d ndarray key
1 parent 6e08c29 commit f2a15c7

File tree

5 files changed

+13
-0
lines changed

5 files changed

+13
-0
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,7 @@ Indexing
945945
- Bug in :meth:`Series.__setitem__` when assigning boolean series with boolean indexer will raise ``LossySetitemError`` (:issue:`57338`)
946946
- Bug in printing :attr:`Index.names` and :attr:`MultiIndex.levels` would not escape single quotes (:issue:`60190`)
947947
- Bug in reindexing of :class:`DataFrame` with :class:`PeriodDtype` columns in case of consolidated block (:issue:`60980`, :issue:`60273`)
948+
- Bug in :meth:`Index.__getitem__` incorrectly raising with a 0-dim ``np.ndarray`` key (:issue:`55601`)
948949

949950
Missing
950951
^^^^^^^

pandas/core/indexes/base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5219,6 +5219,7 @@ def __getitem__(self, key):
52195219
"""
52205220
getitem = self._data.__getitem__
52215221

5222+
key = lib.item_from_zerodim(key)
52225223
if is_integer(key) or is_float(key):
52235224
# GH#44051 exclude bool, which would return a 2d ndarray
52245225
key = com.cast_scalar_indexer(key)

pandas/core/indexes/multi.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2235,6 +2235,7 @@ def __reduce__(self):
22352235
# --------------------------------------------------------------------
22362236

22372237
def __getitem__(self, key):
2238+
key = lib.item_from_zerodim(key)
22382239
if is_scalar(key):
22392240
key = com.cast_scalar_indexer(key)
22402241

pandas/core/indexes/range.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,6 +1175,7 @@ def __getitem__(self, key):
11751175
"""
11761176
Conserve RangeIndex type for scalar and slice keys.
11771177
"""
1178+
key = lib.item_from_zerodim(key)
11781179
if key is Ellipsis:
11791180
key = slice(None)
11801181
if isinstance(key, slice):

pandas/tests/indexes/test_any_index.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,15 @@ def test_pickle_preserves_name(self, index):
117117

118118

119119
class TestIndexing:
120+
def test_getitem_0d_ndarray(self, index):
121+
# GH#55601
122+
if len(index) == 0:
123+
pytest.skip(reason="Test assumes non-empty index")
124+
key = np.array(0)
125+
result = index[key]
126+
127+
assert result == index[0]
128+
120129
def test_get_loc_listlike_raises_invalid_index_error(self, index):
121130
# and never TypeError
122131
key = np.array([0, 1], dtype=np.intp)

0 commit comments

Comments
 (0)