Skip to content

Commit

Permalink
Backport PR #53102 on branch 2.0.x (REGR: MultiIndex.join not resorti…
Browse files Browse the repository at this point in the history
…ng levels of new index) (#53113)

Backport PR #53102: REGR: MultiIndex.join not resorting levels of new index

Co-authored-by: Patrick Hoefler <61934744+phofl@users.noreply.github.com>
  • Loading branch information
meeseeksmachine and phofl committed May 6, 2023
1 parent 70d80d8 commit 44f0a9b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v2.0.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Fixed regressions
~~~~~~~~~~~~~~~~~
- Fixed regression in :meth:`DataFrame.loc` losing :class:`MultiIndex` name when enlarging object (:issue:`53053`)
- Fixed regression in :meth:`DataFrame.to_string` printing a backslash at the end of the first row of data, instead of headers, when the DataFrame doesn't fit the line width (:issue:`53054`)
- Fixed regression in :meth:`MultiIndex.join` returning levels in wrong order (:issue:`53093`)
-

.. ---------------------------------------------------------------------------
.. _whatsnew_202.bug_fixes:
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4877,7 +4877,7 @@ def _wrap_joined_index(
mask = lidx == -1
join_idx = self.take(lidx)
right = other.take(ridx)
join_index = join_idx.putmask(mask, right)
join_index = join_idx.putmask(mask, right)._sort_levels_monotonic()
return join_index.set_names(name) # type: ignore[return-value]
else:
name = get_op_result_name(self, other)
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/indexes/multi/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,15 @@ def test_join_dtypes_all_nan(any_numeric_ea_dtype):
]
)
tm.assert_index_equal(result, expected)


def test_join_index_levels():
# GH#53093
midx = midx = MultiIndex.from_tuples([("a", "2019-02-01"), ("a", "2019-02-01")])
midx2 = MultiIndex.from_tuples([("a", "2019-01-31")])
result = midx.join(midx2, how="outer")
expected = MultiIndex.from_tuples(
[("a", "2019-01-31"), ("a", "2019-02-01"), ("a", "2019-02-01")]
)
tm.assert_index_equal(result.levels[1], expected.levels[1])
tm.assert_index_equal(result, expected)

0 comments on commit 44f0a9b

Please sign in to comment.