28
28
29
29
class TestDataFrameAlterAxes (TestData ):
30
30
31
- def test_set_index_manually (self ):
31
+ def test_set_index_directly (self ):
32
32
df = self .mixed_frame .copy ()
33
33
idx = Index (np .arange (len (df ))[::- 1 ])
34
34
@@ -94,7 +94,7 @@ def test_set_index_append(self, drop, keys):
94
94
# A has duplicate values, C does not
95
95
@pytest .mark .parametrize ('keys' , ['A' , 'C' , ['A' , 'B' ]])
96
96
@pytest .mark .parametrize ('drop' , [True , False ])
97
- def test_set_index_append_to_mi (self , drop , keys ):
97
+ def test_set_index_append_to_multiindex (self , drop , keys ):
98
98
# append to existing multiindex
99
99
df = self .dummy .set_index (['D' ], drop = drop , append = True )
100
100
@@ -115,66 +115,64 @@ def test_set_index_after_mutation(self):
115
115
result = df2 .set_index ('key' )
116
116
tm .assert_frame_equal (result , expected )
117
117
118
- @pytest .mark .parametrize ('container' , [Series , Index , np .array , mi ])
119
118
# also test index name if append=True (name is duplicate here for B)
120
- @pytest .mark .parametrize ('append, df_index_name' , [(True , None ),
119
+ @pytest .mark .parametrize ('box' , [Series , Index , np .array , mi ])
120
+ @pytest .mark .parametrize ('append, index_name' , [(True , None ),
121
121
(True , 'B' ), (True , 'test' ), (False , None )])
122
122
@pytest .mark .parametrize ('drop' , [True , False ])
123
- def test_set_index_pass_single_array (self , drop , append , df_index_name ,
124
- container ):
123
+ def test_set_index_pass_single_array (self , drop , append , index_name , box ):
125
124
df = self .dummy .copy ()
126
- df .index .name = df_index_name
125
+ df .index .name = index_name
127
126
128
- key = container (df ['B' ])
127
+ key = box (df ['B' ])
129
128
# np.array and list "forget" the name of B
130
- name = [None if container in [np .array , list ] else 'B' ]
129
+ name = [None if box in [np .array , list ] else 'B' ]
131
130
132
131
result = df .set_index (key , drop = drop , append = append )
133
132
134
133
# only valid column keys are dropped
135
134
# since B is always passed as array above, nothing is dropped
136
135
expected = df .set_index (['B' ], drop = False , append = append )
137
- expected .index .names = [df_index_name ] + name if append else name
136
+ expected .index .names = [index_name ] + name if append else name
138
137
139
138
tm .assert_frame_equal (result , expected )
140
139
141
- @pytest .mark .parametrize ('container' , [Series , Index , np .array , list , mi ])
142
140
# also test index name if append=True (name is duplicate here for A & B)
143
- @pytest .mark .parametrize ('append, df_index_name' ,
141
+ @pytest .mark .parametrize ('box' , [Series , Index , np .array , list , mi ])
142
+ @pytest .mark .parametrize ('append, index_name' ,
144
143
[(True , None ), (True , 'A' ), (True , 'B' ),
145
144
(True , 'test' ), (False , None )])
146
145
@pytest .mark .parametrize ('drop' , [True , False ])
147
- def test_set_index_pass_arrays (self , drop , append , df_index_name ,
148
- container ):
146
+ def test_set_index_pass_arrays (self , drop , append , index_name , box ):
149
147
df = self .dummy .copy ()
150
- df .index .name = df_index_name
148
+ df .index .name = index_name
151
149
152
- keys = ['A' , container (df ['B' ])]
150
+ keys = ['A' , box (df ['B' ])]
153
151
# np.array and list "forget" the name of B
154
- names = ['A' , None if container in [np .array , list ] else 'B' ]
152
+ names = ['A' , None if box in [np .array , list ] else 'B' ]
155
153
156
154
result = df .set_index (keys , drop = drop , append = append )
157
155
158
156
# only valid column keys are dropped
159
157
# since B is always passed as array above, only A is dropped, if at all
160
158
expected = df .set_index (['A' , 'B' ], drop = False , append = append )
161
159
expected = expected .drop ('A' , axis = 1 ) if drop else expected
162
- expected .index .names = [df_index_name ] + names if append else names
160
+ expected .index .names = [index_name ] + names if append else names
163
161
164
162
tm .assert_frame_equal (result , expected )
165
163
166
- @pytest .mark .parametrize ('elem2' , [key , Series , Index , np .array , list , mi ])
167
- @pytest .mark .parametrize ('elem1' , [key , Series , Index , np .array , list , mi ])
168
164
# also test index name if append=True (name is duplicate here for A)
169
- @pytest .mark .parametrize ('append, df_index_name' , [(True , None ),
165
+ @pytest .mark .parametrize ('box1' , [key , Series , Index , np .array , list , mi ])
166
+ @pytest .mark .parametrize ('box2' , [key , Series , Index , np .array , list , mi ])
167
+ @pytest .mark .parametrize ('append, index_name' , [(True , None ),
170
168
(True , 'A' ), (True , 'test' ), (False , None )])
171
169
@pytest .mark .parametrize ('drop' , [True , False ])
172
- def test_set_index_pass_arrays_duplicate (self , drop , append , df_index_name ,
173
- elem1 , elem2 ):
170
+ def test_set_index_pass_arrays_duplicate (self , drop , append , index_name ,
171
+ box1 , box2 ):
174
172
df = self .dummy .copy ()
175
- df .index .name = df_index_name
173
+ df .index .name = index_name
176
174
177
- keys = [elem1 (df ['A' ]), elem2 (df ['A' ])]
175
+ keys = [box1 (df ['A' ]), box2 (df ['A' ])]
178
176
179
177
# == gives ambiguous Boolean for Series
180
178
if keys [0 ] is 'A' and keys [1 ] is 'A' :
@@ -186,15 +184,15 @@ def test_set_index_pass_arrays_duplicate(self, drop, append, df_index_name,
186
184
187
185
# to test against already-tested behavior, we add sequentially,
188
186
# hence second append always True; must wrap in list, otherwise
189
- # list-elements will be illegal
187
+ # list-box will be illegal
190
188
expected = df .set_index ([keys [0 ]], drop = drop , append = append )
191
189
expected = expected .set_index ([keys [1 ]], drop = drop , append = True )
192
190
193
191
tm .assert_frame_equal (result , expected )
194
192
195
193
@pytest .mark .parametrize ('append' , [True , False ])
196
194
@pytest .mark .parametrize ('drop' , [True , False ])
197
- def test_set_index_pass_mi (self , drop , append ):
195
+ def test_set_index_pass_multiindex (self , drop , append ):
198
196
df = self .dummy .copy ()
199
197
keys = MultiIndex .from_arrays ([df ['A' ], df ['B' ]], names = ['A' , 'B' ])
200
198
0 commit comments