@@ -1563,39 +1563,6 @@ def update(self, other):
1563
1563
#----------------------------------------------------------------------
1564
1564
# Reindexing, sorting
1565
1565
1566
- def sort (self , axis = 0 , ascending = True , kind = 'quicksort' , na_position = 'last' ):
1567
- """
1568
- Sort values and index labels by value, in place. For compatibility with
1569
- ndarray API. No return value
1570
-
1571
- Parameters
1572
- ----------
1573
- axis : int (can only be zero)
1574
- ascending : boolean, default True
1575
- Sort ascending. Passing False sorts descending
1576
- kind : {'mergesort', 'quicksort', 'heapsort'}, default 'quicksort'
1577
- Choice of sorting algorithm. See np.sort for more
1578
- information. 'mergesort' is the only stable algorithm
1579
- na_position : {'first', 'last'} (optional, default='last')
1580
- 'first' puts NaNs at the beginning
1581
- 'last' puts NaNs at the end
1582
-
1583
- See Also
1584
- --------
1585
- Series.order
1586
- """
1587
-
1588
- # GH 5856/5863
1589
- if self ._is_cached :
1590
- raise ValueError ("This Series is a view of some other array, to "
1591
- "sort in-place you must create a copy" )
1592
-
1593
- result = self .order (ascending = ascending ,
1594
- kind = kind ,
1595
- na_position = na_position )
1596
-
1597
- self ._update_inplace (result )
1598
-
1599
1566
def sort_index (self , ascending = True ):
1600
1567
"""
1601
1568
Sort object by labels (along an axis)
@@ -1692,9 +1659,38 @@ def rank(self, method='average', na_option='keep', ascending=True,
1692
1659
ascending = ascending , pct = pct )
1693
1660
return self ._constructor (ranks , index = self .index ).__finalize__ (self )
1694
1661
1695
- def order (self , na_last = None , ascending = True , kind = 'quicksort' , na_position = 'last' ):
1662
+ def sort (self , axis = 0 , ascending = True , kind = 'quicksort' , na_position = 'last' , inplace = True ):
1696
1663
"""
1697
- Sorts Series object, by value, maintaining index-value link
1664
+ Sort values and index labels by value. This is an inplace sort by default.
1665
+ Series.order is the equivalent but returns a new Series.
1666
+
1667
+ Parameters
1668
+ ----------
1669
+ axis : int (can only be zero)
1670
+ ascending : boolean, default True
1671
+ Sort ascending. Passing False sorts descending
1672
+ kind : {'mergesort', 'quicksort', 'heapsort'}, default 'quicksort'
1673
+ Choice of sorting algorithm. See np.sort for more
1674
+ information. 'mergesort' is the only stable algorithm
1675
+ na_position : {'first', 'last'} (optional, default='last')
1676
+ 'first' puts NaNs at the beginning
1677
+ 'last' puts NaNs at the end
1678
+ inplace : boolean, default True
1679
+ Do operation in place.
1680
+
1681
+ See Also
1682
+ --------
1683
+ Series.order
1684
+ """
1685
+ return self .order (ascending = ascending ,
1686
+ kind = kind ,
1687
+ na_position = na_position ,
1688
+ inplace = inplace )
1689
+
1690
+ def order (self , na_last = None , ascending = True , kind = 'quicksort' , na_position = 'last' , inplace = False ):
1691
+ """
1692
+ Sorts Series object, by value, maintaining index-value link.
1693
+ This will return a new Series by default. Series.sort is the equivalent but as an inplace method.
1698
1694
1699
1695
Parameters
1700
1696
----------
@@ -1708,6 +1704,8 @@ def order(self, na_last=None, ascending=True, kind='quicksort', na_position='las
1708
1704
na_position : {'first', 'last'} (optional, default='last')
1709
1705
'first' puts NaNs at the beginning
1710
1706
'last' puts NaNs at the end
1707
+ inplace : boolean, default False
1708
+ Do operation in place.
1711
1709
1712
1710
Returns
1713
1711
-------
@@ -1717,6 +1715,12 @@ def order(self, na_last=None, ascending=True, kind='quicksort', na_position='las
1717
1715
--------
1718
1716
Series.sort
1719
1717
"""
1718
+
1719
+ # GH 5856/5853
1720
+ if inplace and self ._is_cached :
1721
+ raise ValueError ("This Series is a view of some other array, to "
1722
+ "sort in-place you must create a copy" )
1723
+
1720
1724
if na_last is not None :
1721
1725
warnings .warn (("na_last is deprecated. Please use na_position instead" ),
1722
1726
FutureWarning )
@@ -1755,8 +1759,13 @@ def _try_kind_sort(arr):
1755
1759
sortedIdx [:n ] = idx [bad ]
1756
1760
else :
1757
1761
raise ValueError ('invalid na_position: {!r}' .format (na_position ))
1758
- return self ._constructor (arr [sortedIdx ], index = self .index [sortedIdx ])\
1759
- .__finalize__ (self )
1762
+
1763
+ result = self ._constructor (arr [sortedIdx ], index = self .index [sortedIdx ])
1764
+
1765
+ if inplace :
1766
+ self ._update_inplace (result )
1767
+ else :
1768
+ return result .__finalize__ (self )
1760
1769
1761
1770
def sortlevel (self , level = 0 , ascending = True ):
1762
1771
"""
0 commit comments