@@ -5174,6 +5174,38 @@ def test_partition_inputs(self):
5174
5174
pcolinfo = pcolinfo .drop ('ID' , errors = 'ignore' ).drop ('Label' , errors = 'ignore' )
5175
5175
self .assertTablesEqual (colinfo [['Two' , 'Model' , 'One' , 'MSRP' ]], pcolinfo )
5176
5176
5177
+ def test_rename (self ):
5178
+ tbl = self .table
5179
+
5180
+ # Rename by name:
5181
+ makeCol = tbl ['Make' ]
5182
+ self .assertTrue (any (col in 'Make' for col in list (tbl .columns )))
5183
+ tbl .rename ({'Make' :'Manufacturer' })
5184
+ # No column named "Make" and a column named "Manufacturer"
5185
+ self .assertFalse (any (col in 'Make' for col in list (tbl .columns )))
5186
+ self .assertTrue (any (col in 'Manufacturer' for col in list (tbl .columns )))
5187
+ # Shouldn't be a "new" column, just 'Make' renamed
5188
+ self .assertEqual (makeCol , tbl ['Manufacturer' ])
5189
+
5190
+ # Rename by function:
5191
+ # Column Manufacturer -> Manufacturer_0
5192
+ tbl .rename (lambda col : col + "_0" )
5193
+ for col in list (tbl .columns ):
5194
+ # Last two characters should be _0 for each col
5195
+ self .assertEqual (col [- 2 :], "_0" )
5196
+
5197
+ # Rename by name for col that doesn't exist
5198
+ # errors='ignore'
5199
+ originalCols = list (copy .deepcopy (tbl .columns ))
5200
+ tbl .rename ({'nope' :'nuh uh' })
5201
+ self .assertFalse (any (col in 'nope' for col in list (tbl .columns )))
5202
+ self .assertListEqual (originalCols , list (tbl .columns ))
5203
+
5204
+ # Rename by name for col that doesn't exist
5205
+ # errors='raise'
5206
+ with self .assertRaises (KeyError ):
5207
+ tbl .rename (tbl .rename ({'nope' :'nuh uh' }, errors = 'raise' ))
5208
+
5177
5209
def test_reset_index (self ):
5178
5210
tbl = self .table
5179
5211
0 commit comments