22
22
23
23
from pandas .tests .frame .common import TestData
24
24
25
- key = lambda x : x .name
26
- mi = lambda x : MultiIndex .from_arrays ([x ])
27
-
28
-
29
25
class TestDataFrameAlterAxes (TestData ):
30
26
31
27
def test_set_index_directly (self ):
@@ -116,14 +112,18 @@ def test_set_index_after_mutation(self):
116
112
tm .assert_frame_equal (result , expected )
117
113
118
114
# 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' ])
120
116
@pytest .mark .parametrize ('append, index_name' , [(True , None ),
121
117
(True , 'B' ), (True , 'test' ), (False , None )])
122
118
@pytest .mark .parametrize ('drop' , [True , False ])
123
119
def test_set_index_pass_single_array (self , drop , append , index_name , box ):
124
120
df = self .dummy .copy ()
125
121
df .index .name = index_name
126
122
123
+ # update constructor in case of MultiIndex
124
+ box = ((lambda x : MultiIndex .from_arrays ([x ]))
125
+ if box == 'MultiIndex' else box )
126
+
127
127
key = box (df ['B' ])
128
128
# np.array and list "forget" the name of B
129
129
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):
138
138
tm .assert_frame_equal (result , expected )
139
139
140
140
# 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' ])
142
142
@pytest .mark .parametrize ('append, index_name' ,
143
143
[(True , None ), (True , 'A' ), (True , 'B' ),
144
144
(True , 'test' ), (False , None )])
@@ -147,6 +147,10 @@ def test_set_index_pass_arrays(self, drop, append, index_name, box):
147
147
df = self .dummy .copy ()
148
148
df .index .name = index_name
149
149
150
+ # update constructor in case of MultiIndex
151
+ box = ((lambda x : MultiIndex .from_arrays ([x ]))
152
+ if box == 'MultiIndex' else box )
153
+
150
154
keys = ['A' , box (df ['B' ])]
151
155
# np.array and list "forget" the name of B
152
156
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):
162
166
tm .assert_frame_equal (result , expected )
163
167
164
168
# 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' ])
167
173
@pytest .mark .parametrize ('append, index_name' , [(True , None ),
168
174
(True , 'A' ), (True , 'test' ), (False , None )])
169
175
@pytest .mark .parametrize ('drop' , [True , False ])
@@ -172,7 +178,15 @@ def test_set_index_pass_arrays_duplicate(self, drop, append, index_name,
172
178
df = self .dummy .copy ()
173
179
df .index .name = index_name
174
180
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' ])]
176
190
177
191
# == gives ambiguous Boolean for Series
178
192
if keys [0 ] is 'A' and keys [1 ] is 'A' :
0 commit comments