Skip to content

Commit 36c85ab

Browse files
authored
Merge pull request #14 from Giftlin/patch-4
ENH: sort=bool keyword argument for index.difference pandas-dev#17839
2 parents b8fe2e8 + 5e1ee8b commit 36c85ab

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

pandas/core/indexes/base.py

+20-18
Original file line numberDiff line numberDiff line change
@@ -2183,7 +2183,7 @@ def _get_consensus_name(self, other):
21832183
return self._shallow_copy(name=name)
21842184
return self
21852185

2186-
def union(self, other):
2186+
def union(self, other, sort=True):
21872187
"""
21882188
Form the union of two Index objects and sorts if possible.
21892189
@@ -2241,27 +2241,29 @@ def union(self, other):
22412241
allow_fill=False)
22422242
result = _concat._concat_compat((self._values, other_diff))
22432243

2244-
try:
2245-
self._values[0] < other_diff[0]
2246-
except TypeError as e:
2247-
warnings.warn("%s, sort order is undefined for "
2248-
"incomparable objects" % e, RuntimeWarning,
2249-
stacklevel=3)
2250-
else:
2251-
types = frozenset((self.inferred_type,
2252-
other.inferred_type))
2253-
if not types & _unsortable_types:
2254-
result.sort()
2244+
if sort:
2245+
try:
2246+
self._values[0] < other_diff[0]
2247+
except TypeError as e:
2248+
warnings.warn("%s, sort order is undefined for "
2249+
"incomparable objects" % e, RuntimeWarning,
2250+
stacklevel=3)
2251+
else:
2252+
types = frozenset((self.inferred_type,
2253+
other.inferred_type))
2254+
if not types & _unsortable_types:
2255+
result.sort()
22552256

22562257
else:
22572258
result = self._values
22582259

2259-
try:
2260-
result = np.sort(result)
2261-
except TypeError as e:
2262-
warnings.warn("%s, sort order is undefined for "
2263-
"incomparable objects" % e, RuntimeWarning,
2264-
stacklevel=3)
2260+
if sort:
2261+
try:
2262+
result = np.sort(result)
2263+
except TypeError as e:
2264+
warnings.warn("%s, sort order is undefined for "
2265+
"incomparable objects" % e, RuntimeWarning,
2266+
stacklevel=3)
22652267

22662268
# for subclasses
22672269
return self._wrap_union_result(other, result)

0 commit comments

Comments
 (0)