@@ -3023,10 +3023,10 @@ def _get_reconciled_name_object(self, other):
3023
3023
3024
3024
@final
3025
3025
def _validate_sort_keyword (self , sort ):
3026
- if sort not in [None , False ]:
3026
+ if sort not in [None , False , True ]:
3027
3027
raise ValueError (
3028
3028
"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."
3030
3030
)
3031
3031
3032
3032
@final
@@ -3070,6 +3070,7 @@ def union(self, other, sort=None):
3070
3070
A RuntimeWarning is issued in this case.
3071
3071
3072
3072
* False : do not sort the result.
3073
+ * True : Sort the result (which may raise TypeError).
3073
3074
3074
3075
Returns
3075
3076
-------
@@ -3154,10 +3155,16 @@ def union(self, other, sort=None):
3154
3155
elif not len (other ) or self .equals (other ):
3155
3156
# NB: whether this (and the `if not len(self)` check below) come before
3156
3157
# 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
3158
3162
3159
3163
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
3161
3168
3162
3169
result = self ._union (other , sort = sort )
3163
3170
@@ -3258,12 +3265,13 @@ def intersection(self, other, sort: bool = False):
3258
3265
Parameters
3259
3266
----------
3260
3267
other : Index or array-like
3261
- sort : False or None, default False
3268
+ sort : True, False or None, default False
3262
3269
Whether to sort the resulting index.
3263
3270
3264
- * False : do not sort the result.
3265
3271
* None : sort the result, except when `self` and `other` are equal
3266
3272
or when the values cannot be compared.
3273
+ * False : do not sort the result.
3274
+ * True : Sort the result (which may raise TypeError).
3267
3275
3268
3276
Returns
3269
3277
-------
@@ -3285,8 +3293,12 @@ def intersection(self, other, sort: bool = False):
3285
3293
3286
3294
if self .equals (other ):
3287
3295
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
3290
3302
3291
3303
if len (self ) == 0 or len (other ) == 0 :
3292
3304
# fastpath; we need to be careful about having commutativity
@@ -3403,14 +3415,15 @@ def difference(self, other, sort=None):
3403
3415
Parameters
3404
3416
----------
3405
3417
other : Index or array-like
3406
- sort : False or None, default None
3418
+ sort : bool or None, default None
3407
3419
Whether to sort the resulting index. By default, the
3408
3420
values are attempted to be sorted, but any TypeError from
3409
3421
incomparable elements is caught by pandas.
3410
3422
3411
3423
* None : Attempt to sort the result, but catch any TypeErrors
3412
3424
from comparing incomparable elements.
3413
3425
* False : Do not sort the result.
3426
+ * True : Sort the result (which may raise TypeError).
3414
3427
3415
3428
Returns
3416
3429
-------
@@ -3439,11 +3452,17 @@ def difference(self, other, sort=None):
3439
3452
3440
3453
if len (other ) == 0 :
3441
3454
# 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
3443
3459
3444
3460
if not self ._should_compare (other ):
3445
3461
# 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
3447
3466
3448
3467
result = self ._difference (other , sort = sort )
3449
3468
return self ._wrap_difference_result (other , result )
@@ -3479,14 +3498,15 @@ def symmetric_difference(self, other, result_name=None, sort=None):
3479
3498
----------
3480
3499
other : Index or array-like
3481
3500
result_name : str
3482
- sort : False or None, default None
3501
+ sort : bool or None, default None
3483
3502
Whether to sort the resulting index. By default, the
3484
3503
values are attempted to be sorted, but any TypeError from
3485
3504
incomparable elements is caught by pandas.
3486
3505
3487
3506
* None : Attempt to sort the result, but catch any TypeErrors
3488
3507
from comparing incomparable elements.
3489
3508
* False : Do not sort the result.
3509
+ * True : Sort the result (which may raise TypeError).
3490
3510
3491
3511
Returns
3492
3512
-------
@@ -7162,10 +7182,12 @@ def unpack_nested_dtype(other: _IndexT) -> _IndexT:
7162
7182
7163
7183
7164
7184
def _maybe_try_sort (result , sort ):
7165
- if sort is None :
7185
+ if sort is not False :
7166
7186
try :
7167
7187
result = algos .safe_sort (result )
7168
7188
except TypeError as err :
7189
+ if sort is True :
7190
+ raise
7169
7191
warnings .warn (
7170
7192
f"{ err } , sort order is undefined for incomparable objects." ,
7171
7193
RuntimeWarning ,
0 commit comments