Skip to content

BUG: Join did not work correctly when one MultiIndex had only one level #37208

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
merged 8 commits into from
Jan 3, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ Reshaping
- Bug in func :meth:`crosstab` when using multiple columns with ``margins=True`` and ``normalize=True`` (:issue:`35144`)
- Bug in :meth:`DataFrame.agg` with ``func={'name':<FUNC>}`` incorrectly raising ``TypeError`` when ``DataFrame.columns==['Name']`` (:issue:`36212`)
- Bug in :meth:`Series.transform` would give incorrect results or raise when the argument ``func`` was dictionary (:issue:`35811`)
- Bug in :func:`join` over :class:`MultiIndex` returned wrong result, when one of both indexes had only one level (:issue:`36909`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to 1.3

-

Sparse
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 @@ -1552,7 +1552,7 @@ def droplevel(self, level=0):

levnums = sorted(self._get_level_number(lev) for lev in level)[::-1]

if len(level) == 0:
if len(level) == 0 and (len(self) > 1 or not isinstance(self, ABCMultiIndex)):
return self
if len(level) >= self.nlevels:
raise ValueError(
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/indexes/multi/test_drop.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,11 @@ def test_drop_not_lexsorted():
tm.assert_index_equal(lexsorted_mi, not_lexsorted_mi)
with tm.assert_produces_warning(PerformanceWarning):
tm.assert_index_equal(lexsorted_mi.drop("a"), not_lexsorted_mi.drop("a"))


def test_droplevel_multiindex_one_level():
# GH: 37208
index = pd.MultiIndex.from_tuples([(2,)], names=("b",))
result = index.droplevel([])
expected = pd.Int64Index([2], name="b")
tm.assert_index_equal(result, expected)
17 changes: 17 additions & 0 deletions pandas/tests/reshape/merge/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -879,3 +879,20 @@ def _join_by_hand(a, b, how="left"):
for col, s in b_re.items():
a_re[col] = s
return a_re.reindex(columns=result_columns)


@pytest.mark.parametrize("how", ["left", "right", "inner", "outer"])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't we have a fixture for this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, yes. thx

def test_join_multiindex_one_level(how):
# GH: 36909
left = pd.DataFrame(
data={"c": 3}, index=pd.MultiIndex.from_tuples([(1, 2)], names=("a", "b"))
)
right = pd.DataFrame(
data={"d": 4}, index=pd.MultiIndex.from_tuples([(2,)], names=("b",))
)
result = left.join(right, how=how)
expected = pd.DataFrame(
{"c": [3], "d": [4]},
index=pd.MultiIndex.from_tuples([(2, 1)], names=["b", "a"]),
)
tm.assert_frame_equal(result, expected)