1
+ import pandas as pd
2
+ import pandas ._testing as tm
3
+ import numpy as np
4
+ import pytest
5
+
6
+
7
+ '''
8
+ This code is meant to test the fix implemented for issue #14467.
9
+ https://github.com/pandas-dev/pandas/issues/14467
10
+ '''
11
+
12
+ class TestNestedListDataMultiIndex :
13
+
14
+ def test_nested_list (self ):
15
+ # Case from issue, creating data from np.array works, and should match this case
16
+ result = pd .DataFrame ([[1 , 2 , 3 ], [4 , 5 , 6 ]],
17
+ index = [['gibberish' ]* 2 , [0 , 1 ]],
18
+ columns = [['baldersash' ]* 3 , [10 , 20 , 30 ]])
19
+
20
+ expected = pd .DataFrame (np .array ([[1 , 2 , 3 ], [4 , 5 , 6 ]]),
21
+ index = [['gibberish' ]* 2 , [0 , 1 ]],
22
+ columns = [['baldersash' ]* 3 , [10 , 20 , 30 ]])
23
+ tm .assert_index_equal (result .columns , expected .columns )
24
+
25
+ def test_nest_list_with_multiIndex (self ):
26
+ # Creating from a multiIndex should also still work
27
+ m = pd .MultiIndex .from_arrays ([['baldersash' ]* 3 , [10 , 20 , 30 ]])
28
+ result = pd .DataFrame ([[1 , 2 , 3 ], [4 , 5 , 6 ]],
29
+ index = [['gibberish' ]* 2 , [0 , 1 ]],
30
+ columns = m )
31
+
32
+ expected = pd .DataFrame (np .array ([[1 , 2 , 3 ], [4 , 5 , 6 ]]),
33
+ index = [['gibberish' ]* 2 , [0 , 1 ]],
34
+ columns = [['baldersash' ]* 3 , [10 , 20 , 30 ]])
35
+ tm .assert_index_equal (result .columns , expected .columns )
36
+
37
+ def test_wrong_length_raises_error (self ):
38
+ # Make sure the code raises an error if the nested lists have the wrong length
39
+ with pytest .raises (ValueError ):
40
+ result = pd .DataFrame ([[1 , 2 , 3 ], [4 , 5 , 6 ]],
41
+ index = [['gibberish' ]* 2 , [0 , 1 ]],
42
+ columns = [['baldersash' ]* 2 , [10 , 20 ]])
0 commit comments