Skip to content

REF: share Index.difference #38676

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 1 commit into from
Dec 24, 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
5 changes: 5 additions & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2906,8 +2906,13 @@ def difference(self, other, sort=None):
other, result_name = self._convert_can_do_setop(other)

if self.equals(other):
# Note: we do not (yet) sort even if sort=None GH#24959
return self[:0].rename(result_name)

if len(other) == 0:
# Note: we do not (yet) sort even if sort=None GH#24959
return self.rename(result_name)

result = self._difference(other, sort=sort)
return self._wrap_setop_result(other, result)

Expand Down
35 changes: 1 addition & 34 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3676,42 +3676,9 @@ def _intersection(self, other, sort=False):
zip(*uniq_tuples), sortorder=0, names=result_names
)

def difference(self, other, sort=None):
"""
Compute set difference of two MultiIndex objects

Parameters
----------
other : MultiIndex
sort : False or None, default None
Sort the resulting MultiIndex if possible

.. versionadded:: 0.24.0

.. versionchanged:: 0.24.1

Changed the default value from ``True`` to ``None``
(without change in behaviour).

Returns
-------
diff : MultiIndex
"""
self._validate_sort_keyword(sort)
self._assert_can_do_setop(other)
def _difference(self, other, sort):
other, result_names = self._convert_can_do_setop(other)

if len(other) == 0:
return self.rename(result_names)

if self.equals(other):
return MultiIndex(
levels=self.levels,
codes=[[]] * self.nlevels,
names=result_names,
verify_integrity=False,
)

this = self._get_unique_index()

indexer = this.get_indexer(other)
Expand Down
10 changes: 0 additions & 10 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,16 +635,6 @@ def _setop(self, other, sort, opname: str):
def _intersection(self, other, sort=False):
return self._setop(other, sort, opname="intersection")

def difference(self, other, sort=None):
self._validate_sort_keyword(sort)
self._assert_can_do_setop(other)
other, result_name = self._convert_can_do_setop(other)

if self.equals(other):
return self[:0].rename(result_name)

return self._difference(other, sort=sort)

def _difference(self, other, sort):

if is_object_dtype(other):
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/indexes/datetimes/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,8 @@ def test_difference(self, tz, sort):
(rng3, other3, expected3),
]:
result_diff = rng.difference(other, sort)
if sort is None:
if sort is None and len(other):
# We dont sort (yet?) when empty GH#24959
expected = expected.sort_values()
tm.assert_index_equal(result_diff, expected)

Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/indexes/multi/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,9 @@ def test_intersection(idx, sort):
# assert result.equals(tuples)


@pytest.mark.parametrize("method", ["intersection", "union"])
@pytest.mark.parametrize(
"method", ["intersection", "union", "difference", "symmetric_difference"]
)
def test_setop_with_categorical(idx, sort, method):
other = idx.to_flat_index().astype("category")
res_names = [None] * idx.nlevels
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/indexes/period/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,8 @@ def test_difference(self, sort):
(rng7, other7, expected7),
]:
result_difference = rng.difference(other, sort=sort)
if sort is None:
if sort is None and len(other):
# We dont sort (yet?) when empty GH#24959
expected = expected.sort_values()
tm.assert_index_equal(result_difference, expected)

Expand Down