Skip to content

REF: de-duplicate algos.try_sort calls #39330

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 2 commits into from
Jan 22, 2021
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
39 changes: 17 additions & 22 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2882,15 +2882,7 @@ def _union(self, other, sort):
else:
result = lvals

if sort is None:
try:
result = algos.safe_sort(result)
except TypeError as err:
warnings.warn(
f"{err}, sort order is undefined for incomparable objects",
RuntimeWarning,
stacklevel=3,
)
result = _maybe_try_sort(result, sort)

return result

Expand Down Expand Up @@ -2998,9 +2990,7 @@ def _intersection(self, other, sort=False):
indexer = indexer.take(mask.nonzero()[0])

result = other.take(indexer).unique()._values

if sort is None:
result = algos.safe_sort(result)
result = _maybe_try_sort(result, sort)

# Intersection has to be unique
assert Index(result).is_unique
Expand Down Expand Up @@ -3070,11 +3060,7 @@ def _difference(self, other, sort):

label_diff = np.setdiff1d(np.arange(this.size), indexer, assume_unique=True)
the_diff = this._values.take(label_diff)
if sort is None:
try:
the_diff = algos.safe_sort(the_diff)
except TypeError:
pass
the_diff = _maybe_try_sort(the_diff, sort)

return the_diff

Expand Down Expand Up @@ -3155,11 +3141,7 @@ def symmetric_difference(self, other, result_name=None, sort=None):
right_diff = other._values.take(right_indexer)

the_diff = concat_compat([left_diff, right_diff])
if sort is None:
try:
the_diff = algos.safe_sort(the_diff)
except TypeError:
pass
the_diff = _maybe_try_sort(the_diff, sort)

return Index(the_diff, name=result_name)

Expand Down Expand Up @@ -6353,3 +6335,16 @@ def unpack_nested_dtype(other: Index) -> Index:
# here too.
return dtype.categories
return other


def _maybe_try_sort(result, sort):
Copy link
Contributor

Choose a reason for hiding this comment

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

if you can add a doc-string at some point

if sort is None:
try:
result = algos.safe_sort(result)
except TypeError as err:
warnings.warn(
f"{err}, sort order is undefined for incomparable objects",
RuntimeWarning,
stacklevel=4,
)
return result
1 change: 1 addition & 0 deletions pandas/tests/indexes/interval/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ def test_symmetric_difference(self, closed, sort):
expected = empty_index(dtype="float64", closed=closed)
tm.assert_index_equal(result, expected)

@pytest.mark.filterwarnings("ignore:'<' not supported between:RuntimeWarning")
@pytest.mark.parametrize(
"op_name", ["union", "intersection", "difference", "symmetric_difference"]
)
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -925,8 +925,9 @@ def test_difference_incomparable(self, opname):
b = Index([2, Timestamp("1999"), 1])
op = operator.methodcaller(opname, b)

# sort=None, the default
result = op(a)
with tm.assert_produces_warning(RuntimeWarning):
# sort=None, the default
result = op(a)
expected = Index([3, Timestamp("2000"), 2, Timestamp("1999")])
if opname == "difference":
expected = expected[:2]
Expand Down