@@ -1563,39 +1563,6 @@ def update(self, other):
15631563 #----------------------------------------------------------------------
15641564 # Reindexing, sorting
15651565
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-
15991566 def sort_index (self , ascending = True ):
16001567 """
16011568 Sort object by labels (along an axis)
@@ -1692,9 +1659,38 @@ def rank(self, method='average', na_option='keep', ascending=True,
16921659 ascending = ascending , pct = pct )
16931660 return self ._constructor (ranks , index = self .index ).__finalize__ (self )
16941661
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 ):
16961663 """
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.
16981694
16991695 Parameters
17001696 ----------
@@ -1708,6 +1704,8 @@ def order(self, na_last=None, ascending=True, kind='quicksort', na_position='las
17081704 na_position : {'first', 'last'} (optional, default='last')
17091705 'first' puts NaNs at the beginning
17101706 'last' puts NaNs at the end
1707+ inplace : boolean, default False
1708+ Do operation in place.
17111709
17121710 Returns
17131711 -------
@@ -1717,6 +1715,12 @@ def order(self, na_last=None, ascending=True, kind='quicksort', na_position='las
17171715 --------
17181716 Series.sort
17191717 """
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+
17201724 if na_last is not None :
17211725 warnings .warn (("na_last is deprecated. Please use na_position instead" ),
17221726 FutureWarning )
@@ -1755,8 +1759,13 @@ def _try_kind_sort(arr):
17551759 sortedIdx [:n ] = idx [bad ]
17561760 else :
17571761 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 )
17601769
17611770 def sortlevel (self , level = 0 , ascending = True ):
17621771 """
0 commit comments