@@ -2522,7 +2522,7 @@ def _m8_to_i8(x):
2522
2522
# Sorting
2523
2523
2524
2524
def sort (self , columns = None , axis = 0 , ascending = True ,
2525
- inplace = False ):
2525
+ inplace = False , kind = 'quicksort' , na_position = 'last' ):
2526
2526
"""
2527
2527
Sort DataFrame either by labels (along either axis) or by the values in
2528
2528
column(s)
@@ -2540,6 +2540,11 @@ def sort(self, columns=None, axis=0, ascending=True,
2540
2540
Sort index/rows versus columns
2541
2541
inplace : boolean, default False
2542
2542
Sort the DataFrame without creating a new instance
2543
+ kind : {'quicksort', 'mergesort', 'heapsort'}, optional
2544
+ This option is only applied when sorting on a single column or label.
2545
+ na_position : {'first', 'last'} (optional, default='last')
2546
+ 'first' puts NaNs at the beginning
2547
+ 'last' puts NaNs at the end
2543
2548
2544
2549
Examples
2545
2550
--------
@@ -2550,10 +2555,10 @@ def sort(self, columns=None, axis=0, ascending=True,
2550
2555
sorted : DataFrame
2551
2556
"""
2552
2557
return self .sort_index (by = columns , axis = axis , ascending = ascending ,
2553
- inplace = inplace )
2558
+ inplace = inplace , kind = kind , na_position = na_position )
2554
2559
2555
2560
def sort_index (self , axis = 0 , by = None , ascending = True , inplace = False ,
2556
- kind = 'quicksort' ):
2561
+ kind = 'quicksort' , na_position = 'last' ):
2557
2562
"""
2558
2563
Sort DataFrame either by labels (along either axis) or by the values in
2559
2564
a column
@@ -2571,6 +2576,11 @@ def sort_index(self, axis=0, by=None, ascending=True, inplace=False,
2571
2576
orders
2572
2577
inplace : boolean, default False
2573
2578
Sort the DataFrame without creating a new instance
2579
+ na_position : {'first', 'last'} (optional, default='last')
2580
+ 'first' puts NaNs at the beginning
2581
+ 'last' puts NaNs at the end
2582
+ kind : {'quicksort', 'mergesort', 'heapsort'}, optional
2583
+ This option is only applied when sorting on a single column or label.
2574
2584
2575
2585
Examples
2576
2586
--------
@@ -2580,8 +2590,8 @@ def sort_index(self, axis=0, by=None, ascending=True, inplace=False,
2580
2590
-------
2581
2591
sorted : DataFrame
2582
2592
"""
2583
- from pandas . core . groupby import _lexsort_indexer
2584
-
2593
+
2594
+ from pandas . core . groupby import _lexsort_indexer , _nargsort
2585
2595
axis = self ._get_axis_number (axis )
2586
2596
if axis not in [0 , 1 ]: # pragma: no cover
2587
2597
raise AssertionError ('Axis must be 0 or 1, got %s' % str (axis ))
@@ -2597,23 +2607,19 @@ def sort_index(self, axis=0, by=None, ascending=True, inplace=False,
2597
2607
if com ._is_sequence (ascending ) and len (by ) != len (ascending ):
2598
2608
raise ValueError ('Length of ascending (%d) != length of by'
2599
2609
' (%d)' % (len (ascending ), len (by )))
2600
-
2601
2610
if len (by ) > 1 :
2602
- keys = []
2603
- for x in by :
2604
- k = self [x ].values
2605
- if k .ndim == 2 :
2606
- raise ValueError ('Cannot sort by duplicate column %s'
2607
- % str (x ))
2608
- keys .append (k )
2609
-
2610
2611
def trans (v ):
2611
2612
if com .needs_i8_conversion (v ):
2612
2613
return v .view ('i8' )
2613
2614
return v
2614
-
2615
- keys = [trans (self [x ].values ) for x in by ]
2616
- indexer = _lexsort_indexer (keys , orders = ascending )
2615
+ keys = []
2616
+ for x in by :
2617
+ k = self [x ].values
2618
+ if k .ndim == 2 :
2619
+ raise ValueError ('Cannot sort by duplicate column %s' % str (x ))
2620
+ keys .append (trans (k ))
2621
+ indexer = _lexsort_indexer (keys , orders = ascending ,
2622
+ na_position = na_position )
2617
2623
indexer = com ._ensure_platform_int (indexer )
2618
2624
else :
2619
2625
by = by [0 ]
@@ -2630,20 +2636,17 @@ def trans(v):
2630
2636
% str (by ))
2631
2637
if isinstance (ascending , (tuple , list )):
2632
2638
ascending = ascending [0 ]
2639
+ indexer = _nargsort (k , kind = kind , ascending = ascending ,
2640
+ na_position = na_position )
2633
2641
2634
- if not ascending :
2635
- k = k [::- 1 ]
2636
- indexer = k .argsort (kind = kind )
2637
- if not ascending :
2638
- indexer = indexer .max () - indexer [::- 1 ]
2639
2642
elif isinstance (labels , MultiIndex ):
2640
- indexer = _lexsort_indexer (labels .labels , orders = ascending )
2643
+ indexer = _lexsort_indexer (labels .labels , orders = ascending ,
2644
+ na_position = na_position )
2641
2645
indexer = com ._ensure_platform_int (indexer )
2642
2646
else :
2643
- indexer = labels .argsort (kind = kind )
2644
- if not ascending :
2645
- indexer = indexer [::- 1 ]
2646
-
2647
+ indexer = _nargsort (labels , kind = kind , ascending = ascending ,
2648
+ na_position = na_position )
2649
+
2647
2650
if inplace :
2648
2651
if axis == 1 :
2649
2652
new_data = self ._data .reindex_items (
0 commit comments