Skip to content

Commit 5e1ee8b

Browse files
authored
ENH: sort=bool keyword argument for index.difference pandas-dev#17839
union
1 parent 9cbf895 commit 5e1ee8b

File tree

1 file changed

+33
-28
lines changed

1 file changed

+33
-28
lines changed

pandas/core/indexes/base.py

+33-28
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)
@@ -2326,7 +2328,7 @@ def intersection(self, other):
23262328
taken.name = None
23272329
return taken
23282330

2329-
def difference(self, other):
2331+
def difference(self, other, sort=True):
23302332
"""
23312333
Return a new Index with elements from the index that are not in
23322334
`other`.
@@ -2366,14 +2368,15 @@ def difference(self, other):
23662368
label_diff = np.setdiff1d(np.arange(this.size), indexer,
23672369
assume_unique=True)
23682370
the_diff = this.values.take(label_diff)
2369-
try:
2370-
the_diff = sorting.safe_sort(the_diff)
2371-
except TypeError:
2372-
pass
2371+
if sort:
2372+
try:
2373+
the_diff = sorting.safe_sort(the_diff)
2374+
except TypeError:
2375+
pass
23732376

23742377
return this._shallow_copy(the_diff, name=result_name, freq=None)
23752378

2376-
def symmetric_difference(self, other, result_name=None):
2379+
def symmetric_difference(self, other, result_name=None, sort=True):
23772380
"""
23782381
Compute the symmetric difference of two Index objects.
23792382
It's sorted if sorting is possible.
@@ -2426,10 +2429,11 @@ def symmetric_difference(self, other, result_name=None):
24262429
right_diff = other.values.take(right_indexer)
24272430

24282431
the_diff = _concat._concat_compat([left_diff, right_diff])
2429-
try:
2430-
the_diff = sorting.safe_sort(the_diff)
2431-
except TypeError:
2432-
pass
2432+
if sort:
2433+
try:
2434+
the_diff = sorting.safe_sort(the_diff)
2435+
except TypeError:
2436+
pass
24332437

24342438
attribs = self._get_attributes_dict()
24352439
attribs['name'] = result_name
@@ -4220,3 +4224,4 @@ def _trim_front(strings):
42204224
def _validate_join_method(method):
42214225
if method not in ['left', 'right', 'inner', 'outer']:
42224226
raise ValueError('do not recognize join method %s' % method)
4227+

0 commit comments

Comments
 (0)