@@ -2409,7 +2409,8 @@ def duplicated(self, cols=None, take_last=False):
2409
2409
#----------------------------------------------------------------------
2410
2410
# Sorting
2411
2411
2412
- def sort (self , columns = None , column = None , axis = 0 , ascending = True ):
2412
+ def sort (self , columns = None , column = None , axis = 0 , ascending = True ,
2413
+ inplace = False ):
2413
2414
"""
2414
2415
Sort DataFrame either by labels (along either axis) or by the values in
2415
2416
column(s)
@@ -2423,6 +2424,8 @@ def sort(self, columns=None, column=None, axis=0, ascending=True):
2423
2424
Sort ascending vs. descending
2424
2425
axis : {0, 1}
2425
2426
Sort index/rows versus columns
2427
+ inplace : boolean, default False
2428
+ Sort the DataFrame without creating a new instance
2426
2429
2427
2430
Returns
2428
2431
-------
@@ -2432,9 +2435,10 @@ def sort(self, columns=None, column=None, axis=0, ascending=True):
2432
2435
import warnings
2433
2436
warnings .warn ("column is deprecated, use columns" , FutureWarning )
2434
2437
columns = column
2435
- return self .sort_index (by = columns , axis = axis , ascending = ascending )
2438
+ return self .sort_index (by = columns , axis = axis , ascending = ascending ,
2439
+ inplace = inplace )
2436
2440
2437
- def sort_index (self , axis = 0 , by = None , ascending = True ):
2441
+ def sort_index (self , axis = 0 , by = None , ascending = True , inplace = False ):
2438
2442
"""
2439
2443
Sort DataFrame either by labels (along either axis) or by the values in
2440
2444
a column
@@ -2448,6 +2452,8 @@ def sort_index(self, axis=0, by=None, ascending=True):
2448
2452
for a nested sort.
2449
2453
ascending : boolean, default True
2450
2454
Sort ascending vs. descending
2455
+ inplace : boolean, default False
2456
+ Sort the DataFrame without creating a new instance
2451
2457
2452
2458
Returns
2453
2459
-------
@@ -2470,7 +2476,18 @@ def sort_index(self, axis=0, by=None, ascending=True):
2470
2476
if not ascending :
2471
2477
indexer = indexer [::- 1 ]
2472
2478
2473
- return self .take (indexer , axis = axis )
2479
+ if inplace :
2480
+ if axis == 1 :
2481
+ self ._data = self ._data .reindex_items (self ._data .items [indexer ],
2482
+ copy = False )
2483
+ elif axis == 0 :
2484
+ self ._data = self ._data .take (indexer )
2485
+ else :
2486
+ raise ValueError ('Axis must be 0 or 1, got %s' % str (axis ))
2487
+ self ._clear_item_cache ()
2488
+ return self
2489
+ else :
2490
+ return self .take (indexer , axis = axis )
2474
2491
2475
2492
def sortlevel (self , level = 0 , axis = 0 , ascending = True ):
2476
2493
"""
0 commit comments