Skip to content
Open
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 @@ -1132,6 +1132,7 @@ Interval

Indexing
^^^^^^^^
- Bug in :func:`pandas.concat` incorrectly constructing the :class:`MultiIndex` when an inner level contained :obj:`pandas.NA` with :class:`pandas.Int64Dtype`, causing a :exc:`KeyError` on lookup (:issue:`62903`)
- Bug in :meth:`DataFrame.__getitem__` returning modified columns when called with ``slice`` in Python 3.12 (:issue:`57500`)
- Bug in :meth:`DataFrame.__getitem__` when slicing a :class:`DataFrame` with many rows raised an ``OverflowError`` (:issue:`59531`)
- Bug in :meth:`DataFrame.__setitem__` on an empty :class:`DataFrame` with a tuple corrupting the frame (:issue:`54385`)
Expand Down
6 changes: 6 additions & 0 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3325,6 +3325,12 @@ def _maybe_to_slice(loc):
try:
return self._engine.get_loc(key)
except KeyError as err:
if any(isna(k) for k in key):
loc, _ = self.get_loc_level(
key, range(self.nlevels), drop_level=False
)
if lib.is_integer(loc):
return loc
raise KeyError(key) from err
except TypeError:
# e.g. test_partial_slicing_with_multiindex partial string slicing
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/indexes/multi/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,14 @@ def test_get_loc_nan(self, level, nulls_fixture):
idx = MultiIndex.from_product(levels)
assert idx.get_loc(tuple(key)) == 3

def test_multiindex_at_lookup_with_na_key(self):
index = MultiIndex(levels=[[1, 2], [2, pd.NA]], codes=[[0, 1], [0, 1]])
df = DataFrame({"a": [1, 2]}, index=index)
result = df.at[(2, pd.NA), "a"]
assert result == 2
loc_result = df.loc[(2, pd.NA), "a"]
assert loc_result == 2

def test_get_loc_missing_nan(self):
# GH 8569
idx = MultiIndex.from_arrays([[1.0, 2.0], [3.0, 4.0]])
Expand Down
Loading