@@ -3023,10 +3023,10 @@ def _get_reconciled_name_object(self, other):
30233023
30243024 @final
30253025 def _validate_sort_keyword (self , sort ):
3026- if sort not in [None , False ]:
3026+ if sort not in [None , False , True ]:
30273027 raise ValueError (
30283028 "The 'sort' keyword only takes the values of "
3029- f"None or False; { sort } was passed."
3029+ f"None, True, or False; { sort } was passed."
30303030 )
30313031
30323032 @final
@@ -3070,6 +3070,7 @@ def union(self, other, sort=None):
30703070 A RuntimeWarning is issued in this case.
30713071
30723072 * False : do not sort the result.
3073+ * True : Sort the result (which may raise TypeError).
30733074
30743075 Returns
30753076 -------
@@ -3154,10 +3155,16 @@ def union(self, other, sort=None):
31543155 elif not len (other ) or self .equals (other ):
31553156 # NB: whether this (and the `if not len(self)` check below) come before
31563157 # or after the is_dtype_equal check above affects the returned dtype
3157- return self ._get_reconciled_name_object (other )
3158+ result = self ._get_reconciled_name_object (other )
3159+ if sort is True :
3160+ return result .sort_values ()
3161+ return result
31583162
31593163 elif not len (self ):
3160- return other ._get_reconciled_name_object (self )
3164+ result = other ._get_reconciled_name_object (self )
3165+ if sort is True :
3166+ return result .sort_values ()
3167+ return result
31613168
31623169 result = self ._union (other , sort = sort )
31633170
@@ -3258,12 +3265,13 @@ def intersection(self, other, sort: bool = False):
32583265 Parameters
32593266 ----------
32603267 other : Index or array-like
3261- sort : False or None, default False
3268+ sort : True, False or None, default False
32623269 Whether to sort the resulting index.
32633270
3264- * False : do not sort the result.
32653271 * None : sort the result, except when `self` and `other` are equal
32663272 or when the values cannot be compared.
3273+ * False : do not sort the result.
3274+ * True : Sort the result (which may raise TypeError).
32673275
32683276 Returns
32693277 -------
@@ -3285,8 +3293,12 @@ def intersection(self, other, sort: bool = False):
32853293
32863294 if self .equals (other ):
32873295 if self .has_duplicates :
3288- return self .unique ()._get_reconciled_name_object (other )
3289- return self ._get_reconciled_name_object (other )
3296+ result = self .unique ()._get_reconciled_name_object (other )
3297+ else :
3298+ result = self ._get_reconciled_name_object (other )
3299+ if sort is True :
3300+ result = result .sort_values ()
3301+ return result
32903302
32913303 if len (self ) == 0 or len (other ) == 0 :
32923304 # fastpath; we need to be careful about having commutativity
@@ -3403,14 +3415,15 @@ def difference(self, other, sort=None):
34033415 Parameters
34043416 ----------
34053417 other : Index or array-like
3406- sort : False or None, default None
3418+ sort : bool or None, default None
34073419 Whether to sort the resulting index. By default, the
34083420 values are attempted to be sorted, but any TypeError from
34093421 incomparable elements is caught by pandas.
34103422
34113423 * None : Attempt to sort the result, but catch any TypeErrors
34123424 from comparing incomparable elements.
34133425 * False : Do not sort the result.
3426+ * True : Sort the result (which may raise TypeError).
34143427
34153428 Returns
34163429 -------
@@ -3439,11 +3452,17 @@ def difference(self, other, sort=None):
34393452
34403453 if len (other ) == 0 :
34413454 # Note: we do not (yet) sort even if sort=None GH#24959
3442- return self .rename (result_name )
3455+ result = self .rename (result_name )
3456+ if sort is True :
3457+ return result .sort_values ()
3458+ return result
34433459
34443460 if not self ._should_compare (other ):
34453461 # Nothing matches -> difference is everything
3446- return self .rename (result_name )
3462+ result = self .rename (result_name )
3463+ if sort is True :
3464+ return result .sort_values ()
3465+ return result
34473466
34483467 result = self ._difference (other , sort = sort )
34493468 return self ._wrap_difference_result (other , result )
@@ -3479,14 +3498,15 @@ def symmetric_difference(self, other, result_name=None, sort=None):
34793498 ----------
34803499 other : Index or array-like
34813500 result_name : str
3482- sort : False or None, default None
3501+ sort : bool or None, default None
34833502 Whether to sort the resulting index. By default, the
34843503 values are attempted to be sorted, but any TypeError from
34853504 incomparable elements is caught by pandas.
34863505
34873506 * None : Attempt to sort the result, but catch any TypeErrors
34883507 from comparing incomparable elements.
34893508 * False : Do not sort the result.
3509+ * True : Sort the result (which may raise TypeError).
34903510
34913511 Returns
34923512 -------
@@ -7162,10 +7182,12 @@ def unpack_nested_dtype(other: _IndexT) -> _IndexT:
71627182
71637183
71647184def _maybe_try_sort (result , sort ):
7165- if sort is None :
7185+ if sort is not False :
71667186 try :
71677187 result = algos .safe_sort (result )
71687188 except TypeError as err :
7189+ if sort is True :
7190+ raise
71697191 warnings .warn (
71707192 f"{ err } , sort order is undefined for incomparable objects." ,
71717193 RuntimeWarning ,
0 commit comments