Skip to content

BUG: join MultiIndex with overlapping IntervalIndex level (#44096) #44588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,7 @@ Reshaping
- Fixed metadata propagation in :meth:`Dataframe.apply` method, consequently fixing the same issue for :meth:`Dataframe.transform`, :meth:`Dataframe.nunique` and :meth:`Dataframe.mode` (:issue:`28283`)
- Bug in :meth:`DataFrame.stack` with ``ExtensionDtype`` columns incorrectly raising (:issue:`43561`)
- Bug in :meth:`Series.unstack` with object doing unwanted type inference on resulting columns (:issue:`44595`)
- Bug in :class:`MultiIndex` failing join operations with overlapping ``IntervalIndex`` levels (:issue:`44096`)
-

Sparse
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/index.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ cdef class BaseMultiIndexCodesEngine:
Integers representing one combination each
"""
zt = [target._get_level_values(i) for i in range(target.nlevels)]
level_codes = [lev.get_indexer(codes) + 1 for lev, codes
level_codes = [lev.get_indexer_for(codes) + 1 for lev, codes
in zip(self.levels, zt)]
return self._codes_to_ints(np.array(level_codes, dtype='uint64').T)

Expand Down
43 changes: 43 additions & 0 deletions pandas/tests/indexes/multi/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from pandas import (
Index,
Interval,
MultiIndex,
)
import pandas._testing as tm
Expand Down Expand Up @@ -115,3 +116,45 @@ def test_join_multi_return_indexers():

result = midx1.join(midx2, return_indexers=False)
tm.assert_index_equal(result, midx1)


def test_join_overlapping_interval_level():
# GH 44096
idx_1 = MultiIndex.from_tuples(
[
(1, Interval(0.0, 1.0)),
(1, Interval(1.0, 2.0)),
(1, Interval(2.0, 5.0)),
(2, Interval(0.0, 1.0)),
(2, Interval(1.0, 3.0)), # interval limit is here at 3.0, not at 2.0
(2, Interval(3.0, 5.0)),
],
names=["num", "interval"],
)

idx_2 = MultiIndex.from_tuples(
[
(1, Interval(2.0, 5.0)),
(1, Interval(0.0, 1.0)),
(1, Interval(1.0, 2.0)),
(2, Interval(3.0, 5.0)),
(2, Interval(0.0, 1.0)),
(2, Interval(1.0, 3.0)),
],
names=["num", "interval"],
)

expected = MultiIndex.from_tuples(
[
(1, Interval(0.0, 1.0)),
(1, Interval(1.0, 2.0)),
(1, Interval(2.0, 5.0)),
(2, Interval(0.0, 1.0)),
(2, Interval(1.0, 3.0)),
(2, Interval(3.0, 5.0)),
],
names=["num", "interval"],
)
result = idx_1.join(idx_2, how="outer")

tm.assert_index_equal(result, expected)