Skip to content

Commit c1da65f

Browse files
committed
ENH: Add sort parameter to other set operations if possible pandas-dev#24471
1 parent 085f27b commit c1da65f

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

pandas/core/indexes/base.py

100644100755
Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2601,7 +2601,7 @@ def _can_union_without_object_cast(self, other) -> bool:
26012601

26022602
@final
26032603
def _validate_sort_keyword(self, sort):
2604-
if sort not in [None, False]:
2604+
if sort not in [None, False, True]:
26052605
raise ValueError(
26062606
"The 'sort' keyword only takes the values of "
26072607
f"None or False; {sort} was passed."
@@ -2662,6 +2662,9 @@ def union(self, other, sort=None):
26622662
self._assert_can_do_setop(other)
26632663
other = ensure_index(other)
26642664

2665+
if sort == True:
2666+
sort = None
2667+
26652668
if not self._can_union_without_object_cast(other):
26662669
return self._union_incompatible_dtypes(other, sort=sort)
26672670

@@ -2790,6 +2793,9 @@ def intersection(self, other, sort=False):
27902793
self._assert_can_do_setop(other)
27912794
other = ensure_index(other)
27922795

2796+
if sort == True:
2797+
sort = None
2798+
27932799
if self.equals(other):
27942800
return self._get_reconciled_name_object(other)
27952801

@@ -2867,6 +2873,9 @@ def difference(self, other, sort=None):
28672873
self._validate_sort_keyword(sort)
28682874
self._assert_can_do_setop(other)
28692875

2876+
if sort == True:
2877+
sort = None
2878+
28702879
if self.equals(other):
28712880
# pass an empty np.ndarray with the appropriate dtype
28722881
return self._shallow_copy(self._data[:0])
@@ -2938,6 +2947,10 @@ def symmetric_difference(self, other, result_name=None, sort=None):
29382947
self._validate_sort_keyword(sort)
29392948
self._assert_can_do_setop(other)
29402949
other, result_name_update = self._convert_can_do_setop(other)
2950+
2951+
if sort == True:
2952+
sort = None
2953+
29412954
if result_name is None:
29422955
result_name = result_name_update
29432956

@@ -5200,8 +5213,13 @@ def _maybe_cast_slice_bound(self, label, side: str_t, kind):
52005213
# We are a plain index here (sub-class override this method if they
52015214
# wish to have special treatment for floats/ints, e.g. Float64Index and
52025215
# datetimelike Indexes
5203-
# reject them, if index does not contain label
5204-
if (is_float(label) or is_integer(label)) and label not in self.values:
5216+
# reject them
5217+
if is_float(label):
5218+
self._invalid_indexer("slice", label)
5219+
5220+
# we are trying to find integer bounds on a non-integer based index
5221+
# this is rejected (generally .loc gets you here)
5222+
elif is_integer(label):
52055223
self._invalid_indexer("slice", label)
52065224

52075225
return label

0 commit comments

Comments
 (0)