Skip to content

Commit 458d756

Browse files
authored
[BUG]: Fix bug in MultiIndex.drop dropped nan when non existing key was given (#37794)
1 parent cc957d1 commit 458d756

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

doc/source/whatsnew/v1.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,7 @@ MultiIndex
621621
- Bug in :meth:`DataFrame.xs` when used with :class:`IndexSlice` raises ``TypeError`` with message ``"Expected label or tuple of labels"`` (:issue:`35301`)
622622
- Bug in :meth:`DataFrame.reset_index` with ``NaT`` values in index raises ``ValueError`` with message ``"cannot convert float NaN to integer"`` (:issue:`36541`)
623623
- Bug in :meth:`DataFrame.combine_first` when used with :class:`MultiIndex` containing string and ``NaN`` values raises ``TypeError`` (:issue:`36562`)
624+
- Bug in :meth:`MultiIndex.drop` dropped ``NaN`` values when non existing key was given as input (:issue:`18853`)
624625

625626
I/O
626627
^^^

pandas/core/indexes/multi.py

+4
Original file line numberDiff line numberDiff line change
@@ -2156,6 +2156,10 @@ def _drop_from_level(self, codes, level, errors="raise"):
21562156
i = self._get_level_number(level)
21572157
index = self.levels[i]
21582158
values = index.get_indexer(codes)
2159+
# If nan should be dropped it will equal -1 here. We have to check which values
2160+
# are not nan and equal -1, this means they are missing in the index
2161+
nan_codes = isna(codes)
2162+
values[(np.equal(nan_codes, False)) & (values == -1)] = -2
21592163

21602164
mask = ~algos.isin(self.codes[i], values)
21612165
if mask.all() and errors != "ignore":

pandas/tests/indexes/multi/test_drop.py

+8
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,11 @@ def test_drop_not_lexsorted():
139139
tm.assert_index_equal(lexsorted_mi, not_lexsorted_mi)
140140
with tm.assert_produces_warning(PerformanceWarning):
141141
tm.assert_index_equal(lexsorted_mi.drop("a"), not_lexsorted_mi.drop("a"))
142+
143+
144+
def test_drop_with_nan_in_index(nulls_fixture):
145+
# GH#18853
146+
mi = MultiIndex.from_tuples([("blah", nulls_fixture)], names=["name", "date"])
147+
msg = r"labels \[Timestamp\('2001-01-01 00:00:00'\)\] not found in level"
148+
with pytest.raises(KeyError, match=msg):
149+
mi.drop(pd.Timestamp("2001"), level="date")

0 commit comments

Comments
 (0)