Skip to content

Allow to union MultiIndex with empty RangeIndex #41275

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 10 commits into from
May 4, 2021
Merged
Show file tree
Hide file tree
Changes from 9 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
6 changes: 4 additions & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2923,8 +2923,10 @@ def union(self, other, sort=None):
other, result_name = self._convert_can_do_setop(other)

if not is_dtype_equal(self.dtype, other.dtype):
if isinstance(self, ABCMultiIndex) and not is_object_dtype(
unpack_nested_dtype(other)
if (
isinstance(self, ABCMultiIndex)
and not is_object_dtype(unpack_nested_dtype(other))
and len(other) > 0
):
raise NotImplementedError(
"Can only union MultiIndex with MultiIndex or Index of tuples, "
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3597,7 +3597,7 @@ def _get_reconciled_name_object(self, other) -> MultiIndex:
def _maybe_match_names(self, other):
"""
Try to find common names to attach to the result of an operation between
a and b. Return a consensus list of names if they match at least partly
a and b. Return a consensus list of names if they match at least partly
or list of None if they have completely different names.
"""
if len(self.names) != len(other.names):
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/indexes/multi/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,14 @@ def test_union_empty_self_different_names():
tm.assert_index_equal(result, expected)


def test_union_multiindex_empty_rangeindex():
# GH#41234
mi = MultiIndex.from_arrays([[1, 2], [3, 4]], names=["a", "b"])
Copy link
Contributor

Choose a reason for hiding this comment

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

test the reverse also

ri = pd.RangeIndex(0)
tm.assert_index_equal(mi, mi.union(ri), check_names=False)
tm.assert_index_equal(mi, ri.union(mi), check_names=False)


@pytest.mark.parametrize(
"method", ["union", "intersection", "difference", "symmetric_difference"]
)
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/reshape/concat/test_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,3 +627,14 @@ def test_concat_null_object_with_dti():
index=exp_index,
)
tm.assert_frame_equal(result, expected)


def test_concat_multiindex_with_empty_rangeindex():
# GH#41234
mi = MultiIndex.from_tuples([("B", 1), ("C", 1)])
df1 = DataFrame([[1, 2]], columns=mi)
df2 = DataFrame(index=[1], columns=pd.RangeIndex(0))

result = concat([df1, df2])
expected = DataFrame([[1, 2], [np.nan, np.nan]], columns=mi)
tm.assert_frame_equal(result, expected)