Skip to content

Commit debf99f

Browse files
phoflluckyvs1
authored andcommitted
BUG: MultiIndex.intersection duplicating nans in result (pandas-dev#38984)
1 parent 856ab94 commit debf99f

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ MultiIndex
262262
^^^^^^^^^^
263263

264264
- Bug in :meth:`DataFrame.drop` raising ``TypeError`` when :class:`MultiIndex` is non-unique and no level is provided (:issue:`36293`)
265+
- Bug in :meth:`MultiIndex.intersection` duplicating ``NaN`` in result (:issue:`38623`)
265266
- Bug in :meth:`MultiIndex.equals` incorrectly returning ``True`` when :class:`MultiIndex` containing ``NaN`` even when they are differntly ordered (:issue:`38439`)
266267
- Bug in :meth:`MultiIndex.intersection` always returning empty when intersecting with :class:`CategoricalIndex` (:issue:`38653`)
267268

pandas/core/indexes/multi.py

+3-10
Original file line numberDiff line numberDiff line change
@@ -3578,16 +3578,9 @@ def _intersection(self, other, sort=False):
35783578
uniq_tuples = algos.unique(inner_tuples)
35793579

35803580
if uniq_tuples is None:
3581-
other_uniq = set(rvals)
3582-
seen = set()
3583-
# pandas\core\indexes\multi.py:3503: error: "add" of "set" does not
3584-
# return a value [func-returns-value]
3585-
uniq_tuples = [
3586-
x
3587-
for x in lvals
3588-
if x in other_uniq
3589-
and not (x in seen or seen.add(x)) # type: ignore[func-returns-value]
3590-
]
3581+
left_unique = self.drop_duplicates()
3582+
indexer = left_unique.get_indexer(other.drop_duplicates())
3583+
uniq_tuples = left_unique.take(np.sort(indexer[indexer != -1]))
35913584

35923585
if sort is None:
35933586
uniq_tuples = sorted(uniq_tuples)

pandas/tests/indexes/multi/test_setops.py

+9
Original file line numberDiff line numberDiff line change
@@ -483,3 +483,12 @@ def test_intersection_different_names():
483483
mi2 = MultiIndex.from_arrays([[1], [3]])
484484
result = mi.intersection(mi2)
485485
tm.assert_index_equal(result, mi2)
486+
487+
488+
def test_intersection_with_missing_values_on_both_sides(nulls_fixture):
489+
# GH#38623
490+
mi1 = MultiIndex.from_arrays([[3, nulls_fixture, 4, nulls_fixture], [1, 2, 4, 2]])
491+
mi2 = MultiIndex.from_arrays([[3, nulls_fixture, 3], [1, 2, 4]])
492+
result = mi1.intersection(mi2)
493+
expected = MultiIndex.from_arrays([[3.0, nulls_fixture], [1, 2]])
494+
tm.assert_index_equal(result, expected)

0 commit comments

Comments
 (0)