Skip to content

Commit 0a854b8

Browse files
phofljreback
andauthored
BUG: Fix return of missing values when applying loc to single level of MultiIndex (#37706)
* BUG: Fix return of missing values when applying loc to single level of MultiIndex * Delete duplicate * Add non list case Co-authored-by: Jeff Reback <[email protected]>
1 parent aab60f5 commit 0a854b8

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

doc/source/whatsnew/v1.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,7 @@ Indexing
467467
- Bug in :meth:`Series.__getitem__` when using an unsigned integer array as an indexer giving incorrect results or segfaulting instead of raising ``KeyError`` (:issue:`37218`)
468468
- Bug in :meth:`Index.where` incorrectly casting numeric values to strings (:issue:`37591`)
469469
- Bug in :meth:`Series.loc` and :meth:`DataFrame.loc` raises when numeric label was given for object :class:`Index` although label was in :class:`Index` (:issue:`26491`)
470+
- Bug in :meth:`DataFrame.loc` returned requested key plus missing values when ``loc`` was applied to single level from :class:`MultiIndex` (:issue:`27104`)
470471

471472
Missing
472473
^^^^^^^

pandas/core/indexes/base.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3842,9 +3842,9 @@ def _get_leaf_sorter(labels):
38423842
else:
38433843
left_lev_indexer = ensure_int64(left_lev_indexer)
38443844
rev_indexer = lib.get_reverse_indexer(left_lev_indexer, len(old_level))
3845-
3845+
old_codes = left.codes[level]
38463846
new_lev_codes = algos.take_nd(
3847-
rev_indexer, left.codes[level], allow_fill=False
3847+
rev_indexer, old_codes[old_codes != -1], allow_fill=False
38483848
)
38493849

38503850
new_codes = list(left.codes)

pandas/tests/indexing/multiindex/test_loc.py

+16
Original file line numberDiff line numberDiff line change
@@ -598,3 +598,19 @@ def test_getitem_loc_commutability(multiindex_year_month_day_dataframe_random_da
598598
result = ser[2000, 5]
599599
expected = df.loc[2000, 5]["A"]
600600
tm.assert_series_equal(result, expected)
601+
602+
603+
def test_loc_with_nan():
604+
# GH: 27104
605+
df = DataFrame(
606+
{"col": [1, 2, 5], "ind1": ["a", "d", np.nan], "ind2": [1, 4, 5]}
607+
).set_index(["ind1", "ind2"])
608+
result = df.loc[["a"]]
609+
expected = DataFrame(
610+
{"col": [1]}, index=MultiIndex.from_tuples([("a", 1)], names=["ind1", "ind2"])
611+
)
612+
tm.assert_frame_equal(result, expected)
613+
614+
result = df.loc["a"]
615+
expected = DataFrame({"col": [1]}, index=Index([1], name="ind2"))
616+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)