@@ -181,12 +181,12 @@ def test_getitem_list(self):
181
181
# tuples
182
182
df = DataFrame (randn (8 , 3 ),
183
183
columns = Index ([('foo' , 'bar' ), ('baz' , 'qux' ),
184
- ('peek' , 'aboo' )], name = 'sth' ))
184
+ ('peek' , 'aboo' )], name = [ 'sth' , 'sth2' ] ))
185
185
186
186
result = df [[('foo' , 'bar' ), ('baz' , 'qux' )]]
187
187
expected = df .ix [:, :2 ]
188
188
assert_frame_equal (result , expected )
189
- self .assertEqual (result .columns .name , 'sth' )
189
+ self .assertEqual (result .columns .names , [ 'sth' , 'sth2' ] )
190
190
191
191
def test_setitem_list (self ):
192
192
@@ -2499,6 +2499,31 @@ def test_constructor_dict_of_tuples(self):
2499
2499
expected = DataFrame (dict ((k , list (v )) for k , v in compat .iteritems (data )))
2500
2500
assert_frame_equal (result , expected , check_dtype = False )
2501
2501
2502
+ def test_constructor_dict_multiindex (self ):
2503
+ check = lambda result , expected : tm .assert_frame_equal (
2504
+ result , expected , check_dtype = True , check_index_type = True ,
2505
+ check_column_type = True , check_names = True )
2506
+ d = {('a' , 'a' ): {('i' , 'i' ): 0 , ('i' , 'j' ): 1 , ('j' , 'i' ): 2 },
2507
+ ('b' , 'a' ): {('i' , 'i' ): 6 , ('i' , 'j' ): 5 , ('j' , 'i' ): 4 },
2508
+ ('b' , 'c' ): {('i' , 'i' ): 7 , ('i' , 'j' ): 8 , ('j' , 'i' ): 9 }}
2509
+ _d = sorted (d .items ())
2510
+ df = DataFrame (d )
2511
+ expected = DataFrame (
2512
+ [x [1 ] for x in _d ],
2513
+ index = MultiIndex .from_tuples ([x [0 ] for x in _d ])).T
2514
+ expected .index = MultiIndex .from_tuples (expected .index )
2515
+ check (df , expected )
2516
+
2517
+ d ['z' ] = {'y' : 123. , ('i' , 'i' ): 111 , ('i' , 'j' ): 111 , ('j' , 'i' ): 111 }
2518
+ _d .insert (0 , ('z' , d ['z' ]))
2519
+ expected = DataFrame (
2520
+ [x [1 ] for x in _d ],
2521
+ index = Index ([x [0 ] for x in _d ], tupleize_cols = False )).T
2522
+ expected .index = Index (expected .index , tupleize_cols = False )
2523
+ df = DataFrame (d )
2524
+ df = df .reindex (columns = expected .columns , index = expected .index )
2525
+ check (df , expected )
2526
+
2502
2527
def _check_basic_constructor (self , empty ):
2503
2528
"mat: 2d matrix with shpae (3, 2) to input. empty - makes sized objects"
2504
2529
mat = empty ((2 , 3 ), dtype = float )
@@ -2922,8 +2947,8 @@ class CustomDict(dict):
2922
2947
def test_constructor_ragged (self ):
2923
2948
data = {'A' : randn (10 ),
2924
2949
'B' : randn (8 )}
2925
- assertRaisesRegexp (ValueError , 'arrays must all be same length' ,
2926
- DataFrame , data )
2950
+ with assertRaisesRegexp (ValueError , 'arrays must all be same length' ):
2951
+ DataFrame ( data )
2927
2952
2928
2953
def test_constructor_scalar (self ):
2929
2954
idx = Index (lrange (3 ))
@@ -12105,7 +12130,8 @@ def test_index_namedtuple(self):
12105
12130
IndexType = namedtuple ("IndexType" , ["a" , "b" ])
12106
12131
idx1 = IndexType ("foo" , "bar" )
12107
12132
idx2 = IndexType ("baz" , "bof" )
12108
- index = Index ([idx1 , idx2 ], name = "composite_index" )
12133
+ index = Index ([idx1 , idx2 ],
12134
+ name = "composite_index" , tupleize_cols = False )
12109
12135
df = DataFrame ([(1 , 2 ), (3 , 4 )], index = index , columns = ["A" , "B" ])
12110
12136
self .assertEqual (df .ix [IndexType ("foo" , "bar" )]["A" ], 1 )
12111
12137
0 commit comments