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 @@ -945,6 +945,7 @@ Indexing
- Bug in :meth:`Series.__setitem__` when assigning boolean series with boolean indexer will raise ``LossySetitemError`` (:issue:`57338`)
- Bug in printing :attr:`Index.names` and :attr:`MultiIndex.levels` would not escape single quotes (:issue:`60190`)
- Bug in reindexing of :class:`DataFrame` with :class:`PeriodDtype` columns in case of consolidated block (:issue:`60980`, :issue:`60273`)
- Bug in :meth:`Index.__getitem__` incorrectly raising with a 0-dim ``np.ndarray`` key (:issue:`55601`)

Missing
^^^^^^^
Expand Down
1 change: 1 addition & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5219,6 +5219,7 @@ def __getitem__(self, key):
"""
getitem = self._data.__getitem__

key = lib.item_from_zerodim(key)
if is_integer(key) or is_float(key):
# GH#44051 exclude bool, which would return a 2d ndarray
key = com.cast_scalar_indexer(key)
Expand Down
1 change: 1 addition & 0 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2235,6 +2235,7 @@ def __reduce__(self):
# --------------------------------------------------------------------

def __getitem__(self, key):
key = lib.item_from_zerodim(key)
if is_scalar(key):
key = com.cast_scalar_indexer(key)

Expand Down
1 change: 1 addition & 0 deletions pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,7 @@ def __getitem__(self, key):
"""
Conserve RangeIndex type for scalar and slice keys.
"""
key = lib.item_from_zerodim(key)
if key is Ellipsis:
key = slice(None)
if isinstance(key, slice):
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/indexes/test_any_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ def test_pickle_preserves_name(self, index):


class TestIndexing:
def test_getitem_0d_ndarray(self, index):
# GH#55601
if len(index) == 0:
pytest.skip(reason="Test assumes non-empty index")
key = np.array(0)
result = index[key]

assert result == index[0]

def test_get_loc_listlike_raises_invalid_index_error(self, index):
# and never TypeError
key = np.array([0, 1], dtype=np.intp)
Expand Down
Loading