@@ -2245,6 +2245,11 @@ def _get_reconciled_name_object(self, other):
2245
2245
return self ._shallow_copy (name = name )
2246
2246
return self
2247
2247
2248
+ def _validate_sort_keyword (self , sort ):
2249
+ if sort not in [None , False ]:
2250
+ raise ValueError ("The 'sort' keyword only takes the values of "
2251
+ "None or False; {0} was passed." .format (sort ))
2252
+
2248
2253
def union (self , other , sort = None ):
2249
2254
"""
2250
2255
Form the union of two Index objects.
@@ -2262,16 +2267,14 @@ def union(self, other, sort=None):
2262
2267
3. Some values in `self` or `other` cannot be compared.
2263
2268
A RuntimeWarning is issued in this case.
2264
2269
2265
- * True : sort the result. A TypeError is raised when the
2266
- values cannot be compared.
2267
2270
* False : do not sort the result.
2268
2271
2269
2272
.. versionadded:: 0.24.0
2270
2273
2271
2274
.. versionchanged:: 0.24.1
2272
2275
2273
- Changed the default `sort` to None, matching the
2274
- behavior of pandas 0.23.4 and earlier .
2276
+ Changed the default `sort` from True to None (without
2277
+ change in behaviour) .
2275
2278
2276
2279
Returns
2277
2280
-------
@@ -2285,20 +2288,15 @@ def union(self, other, sort=None):
2285
2288
>>> idx1.union(idx2)
2286
2289
Int64Index([1, 2, 3, 4, 5, 6], dtype='int64')
2287
2290
"""
2291
+ self ._validate_sort_keyword (sort )
2288
2292
self ._assert_can_do_setop (other )
2289
2293
other = ensure_index (other )
2290
2294
2291
2295
if len (other ) == 0 or self .equals (other ):
2292
- result = self ._get_reconciled_name_object (other )
2293
- if sort :
2294
- result = result .sort_values ()
2295
- return result
2296
+ return self ._get_reconciled_name_object (other )
2296
2297
2297
2298
if len (self ) == 0 :
2298
- result = other ._get_reconciled_name_object (self )
2299
- if sort :
2300
- result = result .sort_values ()
2301
- return result
2299
+ return other ._get_reconciled_name_object (self )
2302
2300
2303
2301
# TODO: is_dtype_union_equal is a hack around
2304
2302
# 1. buggy set ops with duplicates (GH #13432)
@@ -2348,9 +2346,6 @@ def union(self, other, sort=None):
2348
2346
warnings .warn ("{}, sort order is undefined for "
2349
2347
"incomparable objects" .format (e ),
2350
2348
RuntimeWarning , stacklevel = 3 )
2351
- elif sort :
2352
- # raise if not sortable.
2353
- result = sorting .safe_sort (result )
2354
2349
2355
2350
# for subclasses
2356
2351
return self ._wrap_setop_result (other , result )
@@ -2367,12 +2362,12 @@ def intersection(self, other, sort=False):
2367
2362
Parameters
2368
2363
----------
2369
2364
other : Index or array-like
2370
- sort : bool or None, default False
2365
+ sort : False or None, default False
2371
2366
Whether to sort the resulting index.
2372
2367
2373
2368
* False : do not sort the result.
2374
- * True : sort the result. A TypeError is raised when the
2375
- values cannot be compared.
2369
+ * None : sort the result, except when `self` and `other` are equal
2370
+ or when the values cannot be compared.
2376
2371
2377
2372
.. versionadded:: 0.24.0
2378
2373
@@ -2392,14 +2387,12 @@ def intersection(self, other, sort=False):
2392
2387
>>> idx1.intersection(idx2)
2393
2388
Int64Index([3, 4], dtype='int64')
2394
2389
"""
2390
+ self ._validate_sort_keyword (sort )
2395
2391
self ._assert_can_do_setop (other )
2396
2392
other = ensure_index (other )
2397
2393
2398
2394
if self .equals (other ):
2399
- result = self ._get_reconciled_name_object (other )
2400
- if sort :
2401
- result = result .sort_values ()
2402
- return result
2395
+ return self ._get_reconciled_name_object (other )
2403
2396
2404
2397
if not is_dtype_equal (self .dtype , other .dtype ):
2405
2398
this = self .astype ('O' )
@@ -2434,7 +2427,7 @@ def intersection(self, other, sort=False):
2434
2427
2435
2428
taken = other .take (indexer )
2436
2429
2437
- if sort :
2430
+ if sort is None :
2438
2431
taken = sorting .safe_sort (taken .values )
2439
2432
if self .name != other .name :
2440
2433
name = None
@@ -2457,22 +2450,20 @@ def difference(self, other, sort=None):
2457
2450
Parameters
2458
2451
----------
2459
2452
other : Index or array-like
2460
- sort : bool or None, default None
2453
+ sort : False or None, default None
2461
2454
Whether to sort the resulting index. By default, the
2462
2455
values are attempted to be sorted, but any TypeError from
2463
2456
incomparable elements is caught by pandas.
2464
2457
2465
2458
* None : Attempt to sort the result, but catch any TypeErrors
2466
2459
from comparing incomparable elements.
2467
2460
* False : Do not sort the result.
2468
- * True : Sort the result, raising a TypeError if any elements
2469
- cannot be compared.
2470
2461
2471
2462
.. versionadded:: 0.24.0
2472
2463
2473
2464
.. versionchanged:: 0.24.1
2474
2465
2475
- Added the `None` option , which matches the behavior of
2466
+ Changed `True` to `None`, which matches the behavior of
2476
2467
pandas 0.23.4 and earlier.
2477
2468
2478
2469
Returns
@@ -2489,6 +2480,7 @@ def difference(self, other, sort=None):
2489
2480
>>> idx1.difference(idx2, sort=False)
2490
2481
Int64Index([2, 1], dtype='int64')
2491
2482
"""
2483
+ self ._validate_sort_keyword (sort )
2492
2484
self ._assert_can_do_setop (other )
2493
2485
2494
2486
if self .equals (other ):
@@ -2510,8 +2502,6 @@ def difference(self, other, sort=None):
2510
2502
the_diff = sorting .safe_sort (the_diff )
2511
2503
except TypeError :
2512
2504
pass
2513
- elif sort :
2514
- the_diff = sorting .safe_sort (the_diff )
2515
2505
2516
2506
return this ._shallow_copy (the_diff , name = result_name , freq = None )
2517
2507
@@ -2523,22 +2513,20 @@ def symmetric_difference(self, other, result_name=None, sort=None):
2523
2513
----------
2524
2514
other : Index or array-like
2525
2515
result_name : str
2526
- sort : bool or None, default None
2516
+ sort : False or None, default None
2527
2517
Whether to sort the resulting index. By default, the
2528
2518
values are attempted to be sorted, but any TypeError from
2529
2519
incomparable elements is caught by pandas.
2530
2520
2531
2521
* None : Attempt to sort the result, but catch any TypeErrors
2532
2522
from comparing incomparable elements.
2533
2523
* False : Do not sort the result.
2534
- * True : Sort the result, raising a TypeError if any elements
2535
- cannot be compared.
2536
2524
2537
2525
.. versionadded:: 0.24.0
2538
2526
2539
2527
.. versionchanged:: 0.24.1
2540
2528
2541
- Added the `None` option , which matches the behavior of
2529
+ Changed `True` to `None`, which matches the behavior of
2542
2530
pandas 0.23.4 and earlier.
2543
2531
2544
2532
Returns
@@ -2564,6 +2552,7 @@ def symmetric_difference(self, other, result_name=None, sort=None):
2564
2552
>>> idx1 ^ idx2
2565
2553
Int64Index([1, 5], dtype='int64')
2566
2554
"""
2555
+ self ._validate_sort_keyword (sort )
2567
2556
self ._assert_can_do_setop (other )
2568
2557
other , result_name_update = self ._convert_can_do_setop (other )
2569
2558
if result_name is None :
@@ -2589,8 +2578,6 @@ def symmetric_difference(self, other, result_name=None, sort=None):
2589
2578
the_diff = sorting .safe_sort (the_diff )
2590
2579
except TypeError :
2591
2580
pass
2592
- elif sort :
2593
- the_diff = sorting .safe_sort (the_diff )
2594
2581
2595
2582
attribs = self ._get_attributes_dict ()
2596
2583
attribs ['name' ] = result_name
0 commit comments