Skip to content

BUG: MultiIndex.symmetric_difference not keeping ea dtype #48607

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 4 commits into from
Sep 19, 2022
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.6.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ MultiIndex
- Bug in :meth:`MultiIndex.unique` losing extension array dtype (:issue:`48335`)
- Bug in :meth:`MultiIndex.union` losing extension array (:issue:`48498`, :issue:`48505`)
- Bug in :meth:`MultiIndex.append` not checking names for equality (:issue:`48288`)
- Bug in :meth:`MultiIndex.symmetric_difference` losing extension array (:issue:`48607`)
-

I/O
Expand Down
30 changes: 11 additions & 19 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3711,31 +3711,23 @@ def symmetric_difference(self, other, result_name=None, sort=None):
left_indexer = np.setdiff1d(
np.arange(this.size), common_indexer, assume_unique=True
)
left_diff = this._values.take(left_indexer)
left_diff = this.take(left_indexer)

# {other} minus {this}
right_indexer = (indexer == -1).nonzero()[0]
right_diff = other._values.take(right_indexer)
right_diff = other.take(right_indexer)

res_values = concat_compat([left_diff, right_diff])
res_values = _maybe_try_sort(res_values, sort)

# pass dtype so we retain object dtype
result = Index(res_values, name=result_name, dtype=res_values.dtype)
res_values = left_diff.append(right_diff)
result = _maybe_try_sort(res_values, sort)

if self._is_multi:
self = cast("MultiIndex", self)
if not self._is_multi:
return Index(result, name=result_name, dtype=res_values.dtype)
else:
left_diff = cast("MultiIndex", left_diff)
if len(result) == 0:
# On equal symmetric_difference MultiIndexes the difference is empty.
# Therefore, an empty MultiIndex is returned GH#13490
return type(self)(
levels=[[] for _ in range(self.nlevels)],
codes=[[] for _ in range(self.nlevels)],
names=result.name,
)
return type(self).from_tuples(result, names=result.name)

return result
# result might be an Index, if other was an Index
return left_diff.remove_unused_levels().set_names(result_name)
return result.set_names(result_name)

@final
def _assert_can_do_setop(self, other) -> bool:
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/indexes/multi/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,22 @@ def test_setops_disallow_true(method):
getattr(idx1, method)(idx2, sort=True)


@pytest.mark.parametrize("val", [pd.NA, 5])
def test_symmetric_difference_keeping_ea_dtype(any_numeric_ea_dtype, val):
# GH#48607
midx = MultiIndex.from_arrays(
[Series([1, 2], dtype=any_numeric_ea_dtype), [2, 1]], names=["a", None]
)
midx2 = MultiIndex.from_arrays(
[Series([1, 2, val], dtype=any_numeric_ea_dtype), [1, 1, 3]]
)
result = midx.symmetric_difference(midx2)
expected = MultiIndex.from_arrays(
[Series([1, 1, val], dtype=any_numeric_ea_dtype), [1, 2, 3]]
)
tm.assert_index_equal(result, expected)


@pytest.mark.parametrize(
("tuples", "exp_tuples"),
[
Expand Down