#NEWLINE# def sort_values( self , inplace = False , ascending = True , na_position = 'last' ) : #NEWLINE# #INDENT# " Sorts the Categorical by category value returning a new\n        Categorical by default.\n\n        While an ordering is applied to the category values, sorting in this\n        context refers more to organizing and grouping together based on\n        matching category values. Thus, this function can be called on an\n        unordered Categorical instance unlike the functions 'Categorical.min'\n        and 'Categorical.max'.\n\n        Parameters\n        ----------\n        inplace : boolean, default False\n            Do operation in place.\n        ascending : boolean, default True\n            Order ascending. Passing False orders descending. The\n            ordering parameter provides the method by which the\n            category values are organized.\n        na_position : {'first', 'last'} (optional, default='last')\n            'first' puts NaNs at the beginning\n            'last' puts NaNs at the end\n\n        Returns\n        -------\n        y : Categorical or None\n\n        See Also\n        --------\n        Categorical.sort\n\n        Examples\n        --------\n        >>> c = pd.Categorical([1, 2, 2, 1, 5])\n        >>> c\n        [1, 2, 2, 1, 5]\n        Categories (3, int64): [1, 2, 5]\n        >>> c.sort_values()\n        [1, 1, 2, 2, 5]\n        Categories (3, int64): [1, 2, 5]\n        >>> c.sort_values(ascending=False)\n        [5, 2, 2, 1, 1]\n        Categories (3, int64): [1, 2, 5]\n\n        Inplace sorting can be done as well:\n\n        >>> c.sort_values(inplace=True)\n        >>> c\n        [1, 1, 2, 2, 5]\n        Categories (3, int64): [1, 2, 5]\n        >>>\n        >>> c = pd.Categorical([1, 2, 2, 1, 5])\n\n        'sort_values' behaviour with NaNs. Note that 'na_position'\n        is independent of the 'ascending' parameter:\n\n        >>> c = pd.Categorical([np.nan, 2, 2, np.nan, 5])\n        >>> c\n        [NaN, 2.0, 2.0, NaN, 5.0]\n        Categories (2, int64): [2, 5]\n        >>> c.sort_values()\n        [2.0, 2.0, 5.0, NaN, NaN]\n        Categories (2, int64): [2, 5]\n        >>> c.sort_values(ascending=False)\n        [5.0, 2.0, 2.0, NaN, NaN]\n        Categories (2, int64): [2, 5]\n        >>> c.sort_values(na_position='first')\n        [NaN, NaN, 2.0, 2.0, 5.0]\n        Categories (2, int64): [2, 5]\n        >>> c.sort_values(ascending=False, na_position='first')\n        [NaN, NaN, 5.0, 2.0, 2.0]\n        Categories (2, int64): [2, 5]\n        " #NEWLINE# if ( na_position not in [ 'last' , 'first' ] ) : #NEWLINE# #INDENT# raise ValueError ( 'invalid na_position: {!r}' . format ( na_position ) ) #NEWLINE# #UNINDENT# codes = np . sort ( self . _codes ) #NEWLINE# if ( not ascending ) : #NEWLINE# #INDENT# codes = codes [ : : ( - 1 ) ] #NEWLINE# #UNINDENT# na_mask = ( codes == ( - 1 ) ) #NEWLINE# if na_mask . any ( ) : #NEWLINE# #INDENT# n_nans = len ( codes [ na_mask ] ) #NEWLINE# if ( na_position == 'first' ) : #NEWLINE# #INDENT# new_codes = codes . copy ( ) #NEWLINE# new_codes [ 0 : n_nans ] = ( - 1 ) #NEWLINE# new_codes [ n_nans : ] = codes [ ( ~ na_mask ) ] #NEWLINE# codes = new_codes #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( na_position == 'last' ) : #NEWLINE# #INDENT# new_codes = codes . copy ( ) #NEWLINE# pos = ( len ( codes ) - n_nans ) #NEWLINE# new_codes [ 0 : pos ] = codes [ ( ~ na_mask ) ] #NEWLINE# new_codes [ pos : ] = ( - 1 ) #NEWLINE# codes = new_codes #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if inplace : #NEWLINE# #INDENT# self . _codes = codes #NEWLINE# return #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return Categorical ( values = codes , categories = self . categories , ordered = self . ordered , fastpath = True )