2222
2323from pandas .tests .frame .common import TestData
2424
25- key = lambda x : x .name
26- mi = lambda x : MultiIndex .from_arrays ([x ])
27-
28-
2925class TestDataFrameAlterAxes (TestData ):
3026
3127 def test_set_index_directly (self ):
@@ -116,14 +112,18 @@ def test_set_index_after_mutation(self):
116112 tm .assert_frame_equal (result , expected )
117113
118114 # also test index name if append=True (name is duplicate here for B)
119- @pytest .mark .parametrize ('box' , [Series , Index , np .array , mi ])
115+ @pytest .mark .parametrize ('box' , [Series , Index , np .array , 'MultiIndex' ])
120116 @pytest .mark .parametrize ('append, index_name' , [(True , None ),
121117 (True , 'B' ), (True , 'test' ), (False , None )])
122118 @pytest .mark .parametrize ('drop' , [True , False ])
123119 def test_set_index_pass_single_array (self , drop , append , index_name , box ):
124120 df = self .dummy .copy ()
125121 df .index .name = index_name
126122
123+ # update constructor in case of MultiIndex
124+ box = ((lambda x : MultiIndex .from_arrays ([x ]))
125+ if box == 'MultiIndex' else box )
126+
127127 key = box (df ['B' ])
128128 # np.array and list "forget" the name of B
129129 name = [None if box in [np .array , list ] else 'B' ]
@@ -138,7 +138,7 @@ def test_set_index_pass_single_array(self, drop, append, index_name, box):
138138 tm .assert_frame_equal (result , expected )
139139
140140 # also test index name if append=True (name is duplicate here for A & B)
141- @pytest .mark .parametrize ('box' , [Series , Index , np .array , list , mi ])
141+ @pytest .mark .parametrize ('box' , [Series , Index , np .array , list , 'MultiIndex' ])
142142 @pytest .mark .parametrize ('append, index_name' ,
143143 [(True , None ), (True , 'A' ), (True , 'B' ),
144144 (True , 'test' ), (False , None )])
@@ -147,6 +147,10 @@ def test_set_index_pass_arrays(self, drop, append, index_name, box):
147147 df = self .dummy .copy ()
148148 df .index .name = index_name
149149
150+ # update constructor in case of MultiIndex
151+ box = ((lambda x : MultiIndex .from_arrays ([x ]))
152+ if box == 'MultiIndex' else box )
153+
150154 keys = ['A' , box (df ['B' ])]
151155 # np.array and list "forget" the name of B
152156 names = ['A' , None if box in [np .array , list ] else 'B' ]
@@ -162,8 +166,10 @@ def test_set_index_pass_arrays(self, drop, append, index_name, box):
162166 tm .assert_frame_equal (result , expected )
163167
164168 # also test index name if append=True (name is duplicate here for A)
165- @pytest .mark .parametrize ('box1' , [key , Series , Index , np .array , list , mi ])
166- @pytest .mark .parametrize ('box2' , [key , Series , Index , np .array , list , mi ])
169+ @pytest .mark .parametrize ('box1' , ['label' , Series , Index , np .array ,
170+ list , 'MultiIndex' ])
171+ @pytest .mark .parametrize ('box2' , ['label' , Series , Index , np .array ,
172+ list , 'MultiIndex' ])
167173 @pytest .mark .parametrize ('append, index_name' , [(True , None ),
168174 (True , 'A' ), (True , 'test' ), (False , None )])
169175 @pytest .mark .parametrize ('drop' , [True , False ])
@@ -172,7 +178,15 @@ def test_set_index_pass_arrays_duplicate(self, drop, append, index_name,
172178 df = self .dummy .copy ()
173179 df .index .name = index_name
174180
175- keys = [box1 (df ['A' ]), box2 (df ['A' ])]
181+ # transform strings to correct box constructor
182+ def rebox (x ):
183+ if x == 'label' :
184+ return lambda x : x .name
185+ elif x == 'MultiIndex' :
186+ return lambda x : MultiIndex .from_arrays ([x ])
187+ return x
188+
189+ keys = [rebox (box1 )(df ['A' ]), rebox (box2 )(df ['A' ])]
176190
177191 # == gives ambiguous Boolean for Series
178192 if keys [0 ] is 'A' and keys [1 ] is 'A' :
0 commit comments