Skip to content

Commit 7c642ac

Browse files
Merge branch 'main' into new-feature
2 parents 09a4702 + 7bfef3b commit 7c642ac

File tree

6 files changed

+23
-8
lines changed

6 files changed

+23
-8
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/core/missing.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,12 @@
4545
)
4646

4747
if TYPE_CHECKING:
48+
from typing import TypeAlias
49+
4850
from pandas import Index
4951

52+
_CubicBC: TypeAlias = Literal["not-a-knot", "clamped", "natural", "periodic"]
53+
5054

5155
def check_value_size(value, mask: npt.NDArray[np.bool_], length: int):
5256
"""
@@ -652,7 +656,7 @@ def _akima_interpolate(
652656
xi: np.ndarray,
653657
yi: np.ndarray,
654658
x: np.ndarray,
655-
der: int | list[int] | None = 0,
659+
der: int = 0,
656660
axis: AxisInt = 0,
657661
):
658662
"""
@@ -673,10 +677,8 @@ def _akima_interpolate(
673677
x : np.ndarray
674678
Of length M.
675679
der : int, optional
676-
How many derivatives to extract; None for all potentially
677-
nonzero derivatives (that is a number equal to the number
678-
of points), or a list of derivatives to extract. This number
679-
includes the function value as 0th derivative.
680+
How many derivatives to extract. This number includes the function
681+
value as 0th derivative.
680682
axis : int, optional
681683
Axis in the yi array corresponding to the x-coordinate values.
682684
@@ -702,9 +704,9 @@ def _cubicspline_interpolate(
702704
yi: np.ndarray,
703705
x: np.ndarray,
704706
axis: AxisInt = 0,
705-
bc_type: str | tuple[Any, Any] = "not-a-knot",
706-
extrapolate=None,
707-
):
707+
bc_type: _CubicBC | tuple[Any, Any] = "not-a-knot",
708+
extrapolate: Literal["periodic"] | bool | None = None,
709+
) -> np.ndarray:
708710
"""
709711
Convenience function for cubic spline data interpolator.
710712

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)