#NEWLINE# def test_sort_index( self ) : #NEWLINE# #INDENT# frame = DataFrame ( np . arange ( 16 ) . reshape ( 4 , 4 ) , index = [ 1 , 2 , 3 , 4 ] , columns = [ 'A' , 'B' , 'C' , 'D' ] ) #NEWLINE# unordered = frame . ix [ [ 3 , 2 , 4 , 1 ] ] #NEWLINE# sorted_df = unordered . sort_index ( axis = 0 ) #NEWLINE# expected = frame #NEWLINE# assert_frame_equal ( sorted_df , expected ) #NEWLINE# sorted_df = unordered . sort_index ( ascending = False ) #NEWLINE# expected = frame [ : : ( - 1 ) ] #NEWLINE# assert_frame_equal ( sorted_df , expected ) #NEWLINE# unordered = frame . ix [ : , [ 'D' , 'B' , 'C' , 'A' ] ] #NEWLINE# sorted_df = unordered . sort_index ( axis = 1 ) #NEWLINE# expected = frame #NEWLINE# assert_frame_equal ( sorted_df , expected ) #NEWLINE# sorted_df = unordered . sort_index ( axis = 1 , ascending = False ) #NEWLINE# expected = frame . ix [ : , : : ( - 1 ) ] #NEWLINE# assert_frame_equal ( sorted_df , expected ) #NEWLINE# sorted_df = frame . sort_values ( by = 'A' ) #NEWLINE# indexer = frame [ 'A' ] . argsort ( ) . values #NEWLINE# expected = frame . ix [ frame . index [ indexer ] ] #NEWLINE# assert_frame_equal ( sorted_df , expected ) #NEWLINE# sorted_df = frame . sort_values ( by = 'A' , ascending = False ) #NEWLINE# indexer = indexer [ : : ( - 1 ) ] #NEWLINE# expected = frame . ix [ frame . index [ indexer ] ] #NEWLINE# assert_frame_equal ( sorted_df , expected ) #NEWLINE# sorted_df = frame . sort_values ( by = 'A' , ascending = False ) #NEWLINE# assert_frame_equal ( sorted_df , expected ) #NEWLINE# sorted_df = frame . sort_values ( by = [ 'A' ] , ascending = [ False ] ) #NEWLINE# assert_frame_equal ( sorted_df , expected ) #NEWLINE# sorted_df = frame . sort_values ( by = 'A' ) #NEWLINE# assert_frame_equal ( sorted_df , expected [ : : ( - 1 ) ] ) #NEWLINE# expected = frame . sort_values ( by = 'A' ) #NEWLINE# assert_frame_equal ( sorted_df , expected ) #NEWLINE# expected = frame . sort_values ( by = [ 'A' , 'B' ] , ascending = False ) #NEWLINE# sorted_df = frame . sort_values ( by = [ 'A' , 'B' ] ) #NEWLINE# assert_frame_equal ( sorted_df , expected [ : : ( - 1 ) ] ) #NEWLINE# self . assertRaises ( ValueError , ( lambda : frame . sort_values ( by = [ 'A' , 'B' ] , axis = 2 , inplace = True ) ) ) #NEWLINE# msg = 'When sorting by column, axis must be 0' #NEWLINE# with assertRaisesRegexp ( ValueError , msg ) : #NEWLINE# #INDENT# frame . sort_values ( by = 'A' , axis = 1 ) #NEWLINE# #UNINDENT# msg = 'Length of ascending \\(5\\) != length of by \\(2\\)' #NEWLINE# with assertRaisesRegexp ( ValueError , msg ) : #NEWLINE# #INDENT# frame . sort_values ( by = [ 'A' , 'B' ] , axis = 0 , ascending = ( [ True ] * 5 ) )