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 5 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.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ Groupby/resample/rolling
Reshaping
^^^^^^^^^
- Bug in :meth:`DataFrame.unstack` with missing levels led to incorrect index names (:issue:`37510`)
- Bug in :func:`join` over :class:`MultiIndex` returned wrong result, when one of both indexes had only one level (:issue:`36909`)
- Bug in :func:`concat` incorrectly casting to ``object`` dtype in some cases when one or more of the operands is empty (:issue:`38843`)
-

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 @@ -1672,7 +1672,7 @@ def _drop_level_numbers(self, levnums: List[int]):
Drop MultiIndex levels by level _number_, not name.
"""

if not levnums:
if not levnums and (len(self) > 1 or not isinstance(self, ABCMultiIndex)):
Copy link
Contributor

Choose a reason for hiding this comment

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

this len(self) > 1 is a very odd check, what is the intent?

Copy link
Member Author

Choose a reason for hiding this comment

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

Seems to be no longer necessary. Code changed quite a bit since I implemented this. Removed it.

return self
if len(levnums) >= 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 @@ -180,3 +180,11 @@ def test_single_level_drop_partially_missing_elements():
msg = r"labels \['a'\] not found in level"
with pytest.raises(KeyError, match=msg):
mi.drop([np.nan, 1, "a"], level=0)


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 @@ -815,3 +815,20 @@ def test_join_cross(input_col, output_cols):
result = left.join(right, how="cross", lsuffix="_x", rsuffix="_y")
expected = DataFrame({output_cols[0]: [1, 1, 3, 3], output_cols[1]: [3, 4, 3, 4]})
tm.assert_frame_equal(result, expected)


@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 = DataFrame(
data={"c": 3}, index=pd.MultiIndex.from_tuples([(1, 2)], names=("a", "b"))
)
right = DataFrame(
data={"d": 4}, index=pd.MultiIndex.from_tuples([(2,)], names=("b",))
)
result = left.join(right, how=how)
expected = DataFrame(
{"c": [3], "d": [4]},
index=pd.MultiIndex.from_tuples([(2, 1)], names=["b", "a"]),
)
tm.assert_frame_equal(result, expected)