@@ -21,17 +21,20 @@ def test_error():
21
21
with pytest .raises (ValueError , match = "columns must be unique" ):
22
22
df .explode ("A" )
23
23
24
+
25
+ def test_error_multi_columns ():
24
26
# GH 39240
27
+ df = pd .DataFrame (
28
+ {"A" : pd .Series ([[0 , 1 , 2 ], np .nan , [], (3 , 4 )], index = list ("abcd" )), "B" : 1 }
29
+ )
25
30
df1 = df .assign (C = [["a" , "b" , "c" ], "foo" , [], ["d" , "e" , "f" ]])
26
31
df1 .columns = list ("ABC" )
27
32
with pytest .raises (ValueError , match = "columns must have matching element counts" ):
28
33
df1 .explode (list ("AC" ))
29
34
30
- # GH 39240
31
35
with pytest .raises (ValueError , match = "column must be nonempty" ):
32
36
df1 .explode ([])
33
37
34
- # GH 39240
35
38
df2 = df .assign (C = [["a" , "b" , "c" ], "foo" , [], "d" ])
36
39
df2 .columns = list ("ABC" )
37
40
with pytest .raises (ValueError , match = "columns must have matching element counts" ):
@@ -207,19 +210,23 @@ def test_multi_columns():
207
210
# GH 39240
208
211
df = pd .DataFrame (
209
212
{
210
- "A" : pd .Series ([[0 , 1 , 2 ], np .nan , [], (3 , 4 )], index = list ("abcd" )),
213
+ "A" : pd .Series (
214
+ [[0 , 1 , 2 ], np .nan , [], (3 , 4 ), np .nan ], index = list ("abcde" )
215
+ ),
211
216
"B" : 1 ,
212
- "C" : [["a" , "b" , "c" ], "foo" , [], ["d" , "e" ]],
217
+ "C" : [["a" , "b" , "c" ], "foo" , [], ["d" , "e" ], np . nan ],
213
218
}
214
219
)
215
220
result = df .explode (list ("AC" ))
216
221
expected = pd .DataFrame (
217
222
{
218
223
"A" : pd .Series (
219
- [0 , 1 , 2 , np .nan , np .nan , 3 , 4 ], index = list ("aaabcdd" ), dtype = object
224
+ [0 , 1 , 2 , np .nan , np .nan , 3 , 4 , np .nan ],
225
+ index = list ("aaabcdde" ),
226
+ dtype = object ,
220
227
),
221
228
"B" : 1 ,
222
- "C" : ["a" , "b" , "c" , "foo" , np .nan , "d" , "e" ],
229
+ "C" : ["a" , "b" , "c" , "foo" , np .nan , "d" , "e" , np . nan ],
223
230
}
224
231
)
225
232
tm .assert_frame_equal (result , expected )
0 commit comments