Skip to content

MultiIndex union/intersection with non-object other #32646

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 5 commits into from
Mar 17, 2020
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
22 changes: 18 additions & 4 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3243,9 +3243,13 @@ def union(self, other, sort=None):

# TODO: Index.union returns other when `len(self)` is 0.

uniq_tuples = lib.fast_unique_multiple(
[self._values, other._ndarray_values], sort=sort
)
if not is_object_dtype(other.dtype):
raise NotImplementedError(
Copy link
Contributor

Choose a reason for hiding this comment

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

is this hit in any tests?

Copy link
Member Author

Choose a reason for hiding this comment

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

no, can add one

Copy link
Contributor

Choose a reason for hiding this comment

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

can you add one

Copy link
Member Author

Choose a reason for hiding this comment

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

added+green (though no azure)

"Can only union MultiIndex with MultiIndex or Index of tuples, "
"try mi.to_flat_index().union(other) instead."
)

uniq_tuples = lib.fast_unique_multiple([self._values, other._values], sort=sort)

return MultiIndex.from_arrays(
zip(*uniq_tuples), sortorder=0, names=result_names
Expand Down Expand Up @@ -3279,8 +3283,18 @@ def intersection(self, other, sort=False):
if self.equals(other):
return self

if not is_object_dtype(other.dtype):
# The intersection is empty
# TODO: we have no tests that get here
return MultiIndex(
levels=self.levels,
codes=[[]] * self.nlevels,
names=result_names,
verify_integrity=False,
)

lvals = self._values
rvals = other._ndarray_values
rvals = other._values

uniq_tuples = None # flag whether _inner_indexer was succesful
if self.is_monotonic and other.is_monotonic:
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/indexes/multi/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ def test_union(idx, sort):
the_union = idx.union(idx[:0], sort=sort)
assert the_union is idx

# FIXME: dont leave commented-out
# won't work in python 3
# tuples = _index.values
# result = _index[:4] | tuples[4:]
Expand Down Expand Up @@ -282,6 +283,7 @@ def test_intersection(idx, sort):
expected = idx[:0]
assert empty.equals(expected)

# FIXME: dont leave commented-out
# can't do in python 3
# tuples = _index.values
# result = _index & tuples
Expand Down Expand Up @@ -351,6 +353,17 @@ def test_union_sort_other_incomparable_sort():
idx.union(idx[:1], sort=True)


def test_union_non_object_dtype_raises():
# GH#32646 raise NotImplementedError instead of less-informative error
mi = pd.MultiIndex.from_product([["a", "b"], [1, 2]])

idx = mi.levels[1]

msg = "Can only union MultiIndex with MultiIndex or Index of tuples"
with pytest.raises(NotImplementedError, match=msg):
mi.union(idx)


@pytest.mark.parametrize(
"method", ["union", "intersection", "difference", "symmetric_difference"]
)
Expand Down