@@ -186,18 +186,19 @@ def test_set_index_pass_arrays_duplicate(self, frame_of_index_cols, drop,
186
186
187
187
# == gives ambiguous Boolean for Series
188
188
if drop and keys [0 ] is 'A' and keys [1 ] is 'A' :
189
- with tm . assert_raises_regex ( KeyError , '.*' ):
190
- df . set_index ( keys , drop = drop , append = append )
189
+ # can't drop same column twice
190
+ first_drop = False
191
191
else :
192
- result = df . set_index ( keys , drop = drop , append = append )
192
+ first_drop = drop
193
193
194
- # to test against already-tested behavior , we add sequentially,
195
- # hence second append always True; must wrap in list, otherwise
196
- # list-box will be illegal
197
- expected = df .set_index ([keys [0 ]], drop = drop , append = append )
198
- expected = expected .set_index ([keys [1 ]], drop = drop , append = True )
194
+ # to test against already-tested behaviour , we add sequentially,
195
+ # hence second append always True; must wrap in list, otherwise
196
+ # list-box will be illegal
197
+ expected = df .set_index ([keys [0 ]], drop = first_drop , append = append )
198
+ expected = expected .set_index ([keys [1 ]], drop = drop , append = True )
199
199
200
- tm .assert_frame_equal (result , expected )
200
+ result = df .set_index (keys , drop = drop , append = append )
201
+ tm .assert_frame_equal (result , expected )
201
202
202
203
@pytest .mark .parametrize ('append' , [True , False ])
203
204
@pytest .mark .parametrize ('drop' , [True , False ])
@@ -229,13 +230,24 @@ def test_set_index_verify_integrity(self, frame_of_index_cols):
229
230
def test_set_index_raise (self , frame_of_index_cols , drop , append ):
230
231
df = frame_of_index_cols
231
232
232
- with tm .assert_raises_regex (KeyError , '.*' ): # column names are A-E
233
+ with tm .assert_raises_regex (KeyError , "['foo', 'bar', 'baz']" ):
234
+ # column names are A-E
233
235
df .set_index (['foo' , 'bar' , 'baz' ], drop = drop , append = append )
234
236
235
237
# non-existent key in list with arrays
236
- with tm .assert_raises_regex (KeyError , '.* ' ):
238
+ with tm .assert_raises_regex (KeyError , 'X ' ):
237
239
df .set_index ([df ['A' ], df ['B' ], 'X' ], drop = drop , append = append )
238
240
241
+ rgx = 'keys may only contain a combination of the following:.*'
242
+ # forbidden type, e.g. set
243
+ with tm .assert_raises_regex (TypeError , rgx ):
244
+ df .set_index (set (df ['A' ]), drop = drop , append = append )
245
+
246
+ # forbidden type in list, e.g. set
247
+ with tm .assert_raises_regex (TypeError , rgx ):
248
+ df .set_index (['A' , df ['A' ], set (df ['A' ])],
249
+ drop = drop , append = append )
250
+
239
251
def test_construction_with_categorical_index (self ):
240
252
ci = tm .makeCategoricalIndex (10 )
241
253
ci .name = 'B'
0 commit comments