@@ -21,21 +21,47 @@ def test_error():
21
21
with pytest .raises (ValueError , match = "columns must be unique" ):
22
22
df .explode ("A" )
23
23
24
- # GH 39240
25
- df1 = df .assign (C = [["a" , "b" , "c" ], "foo" , [], ["d" , "e" , "f" ]])
26
- df1 .columns = list ("ABC" )
27
- with pytest .raises (ValueError , match = "columns must have matching element counts" ):
28
- df1 .explode (list ("AC" ))
29
-
30
- # GH 39240
31
- with pytest .raises (ValueError , match = "column must be nonempty" ):
32
- df1 .explode ([])
33
24
25
+ @pytest .mark .parametrize (
26
+ "input_dict, input_index, input_subset, error_message" ,
27
+ [
28
+ (
29
+ {
30
+ "A" : [[0 , 1 , 2 ], np .nan , [], (3 , 4 )],
31
+ "B" : 1 ,
32
+ "C" : [["a" , "b" , "c" ], "foo" , [], ["d" , "e" , "f" ]],
33
+ },
34
+ list ("abcd" ),
35
+ list ("AC" ),
36
+ "columns must have matching element counts" ,
37
+ ),
38
+ (
39
+ {
40
+ "A" : [[0 , 1 , 2 ], np .nan , [], (3 , 4 )],
41
+ "B" : 1 ,
42
+ "C" : [["a" , "b" , "c" ], "foo" , [], ["d" , "e" , "f" ]],
43
+ },
44
+ list ("abcd" ),
45
+ [],
46
+ "column must be nonempty" ,
47
+ ),
48
+ (
49
+ {
50
+ "A" : [[0 , 1 , 2 ], np .nan , [], (3 , 4 )],
51
+ "B" : 1 ,
52
+ "C" : [["a" , "b" , "c" ], "foo" , [], "d" ],
53
+ },
54
+ list ("abcd" ),
55
+ list ("AC" ),
56
+ "columns must have matching element counts" ,
57
+ ),
58
+ ],
59
+ )
60
+ def test_error_multi_columns (input_dict , input_index , input_subset , error_message ):
34
61
# GH 39240
35
- df2 = df .assign (C = [["a" , "b" , "c" ], "foo" , [], "d" ])
36
- df2 .columns = list ("ABC" )
37
- with pytest .raises (ValueError , match = "columns must have matching element counts" ):
38
- df2 .explode (list ("AC" ))
62
+ df = pd .DataFrame (input_dict , index = input_index )
63
+ with pytest .raises (ValueError , match = error_message ):
64
+ df .explode (input_subset )
39
65
40
66
41
67
def test_basic ():
@@ -203,23 +229,63 @@ def test_explode_sets():
203
229
tm .assert_frame_equal (result , expected )
204
230
205
231
206
- def test_multi_columns ():
232
+ @pytest .mark .parametrize (
233
+ "input_dict, input_index, input_subset, expected_dict, expected_index" ,
234
+ [
235
+ (
236
+ {
237
+ "A" : [[0 , 1 , 2 ], np .nan , [], (3 , 4 ), np .nan ],
238
+ "B" : 1 ,
239
+ "C" : [["a" , "b" , "c" ], "foo" , [], ["d" , "e" ], np .nan ],
240
+ },
241
+ list ("abcde" ),
242
+ list ("AC" ),
243
+ {
244
+ "A" : pd .Series (
245
+ [0 , 1 , 2 , np .nan , np .nan , 3 , 4 , np .nan ],
246
+ index = list ("aaabcdde" ),
247
+ dtype = object ,
248
+ ),
249
+ "B" : 1 ,
250
+ "C" : ["a" , "b" , "c" , "foo" , np .nan , "d" , "e" , np .nan ],
251
+ },
252
+ list ("aaabcdde" ),
253
+ ),
254
+ (
255
+ {
256
+ "A" : [[0 , 1 , 2 ], np .nan , [], (3 , 4 ), np .nan ],
257
+ "B" : 1 ,
258
+ "C" : [["a" , "b" , "c" ], "foo" , [], ["d" , "e" ], np .nan ],
259
+ },
260
+ list ("abcde" ),
261
+ list ("A" ),
262
+ {
263
+ "A" : pd .Series (
264
+ [0 , 1 , 2 , np .nan , np .nan , 3 , 4 , np .nan ],
265
+ index = list ("aaabcdde" ),
266
+ dtype = object ,
267
+ ),
268
+ "B" : 1 ,
269
+ "C" : [
270
+ ["a" , "b" , "c" ],
271
+ ["a" , "b" , "c" ],
272
+ ["a" , "b" , "c" ],
273
+ "foo" ,
274
+ [],
275
+ ["d" , "e" ],
276
+ ["d" , "e" ],
277
+ np .nan ,
278
+ ],
279
+ },
280
+ list ("aaabcdde" ),
281
+ ),
282
+ ],
283
+ )
284
+ def test_multi_columns (
285
+ input_dict , input_index , input_subset , expected_dict , expected_index
286
+ ):
207
287
# GH 39240
208
- df = pd .DataFrame (
209
- {
210
- "A" : pd .Series ([[0 , 1 , 2 ], np .nan , [], (3 , 4 )], index = list ("abcd" )),
211
- "B" : 1 ,
212
- "C" : [["a" , "b" , "c" ], "foo" , [], ["d" , "e" ]],
213
- }
214
- )
215
- result = df .explode (list ("AC" ))
216
- expected = pd .DataFrame (
217
- {
218
- "A" : pd .Series (
219
- [0 , 1 , 2 , np .nan , np .nan , 3 , 4 ], index = list ("aaabcdd" ), dtype = object
220
- ),
221
- "B" : 1 ,
222
- "C" : ["a" , "b" , "c" , "foo" , np .nan , "d" , "e" ],
223
- }
224
- )
288
+ df = pd .DataFrame (input_dict , index = input_index )
289
+ result = df .explode (input_subset )
290
+ expected = pd .DataFrame (expected_dict , expected_index )
225
291
tm .assert_frame_equal (result , expected )
0 commit comments