#NEWLINE# def test_rename( self ) : #NEWLINE# #INDENT# mapping = { #NEWLINE# #INDENT# 'A' : 'a' , #NEWLINE# 'B' : 'b' , #NEWLINE# 'C' : 'c' , #NEWLINE# 'D' : 'd' , #NEWLINE# #UNINDENT# } #NEWLINE# renamed = self . frame . rename ( columns = mapping ) #NEWLINE# renamed2 = self . frame . rename ( columns = str . lower ) #NEWLINE# assert_frame_equal ( renamed , renamed2 ) #NEWLINE# assert_frame_equal ( renamed2 . rename ( columns = str . upper ) , self . frame , check_names = False ) #NEWLINE# data = { #NEWLINE# #INDENT# 'A' : { #NEWLINE# #INDENT# 'foo' : 0 , #NEWLINE# 'bar' : 1 , #NEWLINE# #UNINDENT# } , #NEWLINE# #UNINDENT# } #NEWLINE# df = DataFrame ( data ) #NEWLINE# renamed = df . rename ( index = { #NEWLINE# #INDENT# 'foo' : 'bar' , #NEWLINE# 'bar' : 'foo' , #NEWLINE# #UNINDENT# } ) #NEWLINE# self . assert_numpy_array_equal ( renamed . index , [ 'foo' , 'bar' ] ) #NEWLINE# renamed = df . rename ( index = str . upper ) #NEWLINE# self . assert_numpy_array_equal ( renamed . index , [ 'BAR' , 'FOO' ] ) #NEWLINE# self . assertRaises ( TypeError , self . frame . rename ) #NEWLINE# renamed = self . frame . rename ( columns = { #NEWLINE# #INDENT# 'C' : 'foo' , #NEWLINE# 'D' : 'bar' , #NEWLINE# #UNINDENT# } ) #NEWLINE# self . assert_numpy_array_equal ( renamed . columns , [ 'A' , 'B' , 'foo' , 'bar' ] ) #NEWLINE# renamed = self . frame . T . rename ( index = { #NEWLINE# #INDENT# 'C' : 'foo' , #NEWLINE# 'D' : 'bar' , #NEWLINE# #UNINDENT# } ) #NEWLINE# self . assert_numpy_array_equal ( renamed . index , [ 'A' , 'B' , 'foo' , 'bar' ] ) #NEWLINE# index = Index ( [ 'foo' , 'bar' ] , name = 'name' ) #NEWLINE# renamer = DataFrame ( data , index = index ) #NEWLINE# renamed = renamer . rename ( index = { #NEWLINE# #INDENT# 'foo' : 'bar' , #NEWLINE# 'bar' : 'foo' , #NEWLINE# #UNINDENT# } ) #NEWLINE# self . assert_numpy_array_equal ( renamed . index , [ 'bar' , 'foo' ] ) #NEWLINE# self . assertEqual ( renamed . index . name , renamer . index . name ) #NEWLINE# tuples_index = [ ( 'foo1' , 'bar1' ) , ( 'foo2' , 'bar2' ) ] #NEWLINE# tuples_columns = [ ( 'fizz1' , 'buzz1' ) , ( 'fizz2' , 'buzz2' ) ] #NEWLINE# index = MultiIndex . from_tuples ( tuples_index , names = [ 'foo' , 'bar' ] ) #NEWLINE# columns = MultiIndex . from_tuples ( tuples_columns , names = [ 'fizz' , 'buzz' ] ) #NEWLINE# renamer = DataFrame ( [ ( 0 , 0 ) , ( 1 , 1 ) ] , index = index , columns = columns ) #NEWLINE# renamed = renamer . rename ( index = { #NEWLINE# #INDENT# 'foo1' : 'foo3' , #NEWLINE# 'bar2' : 'bar3' , #NEWLINE# #UNINDENT# } , columns = { #NEWLINE# #INDENT# 'fizz1' : 'fizz3' , #NEWLINE# 'buzz2' : 'buzz3' , #NEWLINE# #UNINDENT# } ) #NEWLINE# new_index = MultiIndex . from_tuples ( [ ( 'foo3' , 'bar1' ) , ( 'foo2' , 'bar3' ) ] ) #NEWLINE# new_columns = MultiIndex . from_tuples ( [ ( 'fizz3' , 'buzz1' ) , ( 'fizz2' , 'buzz3' ) ] ) #NEWLINE# self . assert_numpy_array_equal ( renamed . index , new_index ) #NEWLINE# self . assert_numpy_array_equal ( renamed . columns , new_columns ) #NEWLINE# self . assertEqual ( renamed . index . names , renamer . index . names ) #NEWLINE# self . assertEqual ( renamed . columns . names , renamer . columns . names )