@@ -181,12 +181,12 @@ def test_getitem_list(self):
181181 # tuples
182182 df = DataFrame (randn (8 , 3 ),
183183 columns = Index ([('foo' , 'bar' ), ('baz' , 'qux' ),
184- ('peek' , 'aboo' )], name = 'sth' ))
184+ ('peek' , 'aboo' )], name = [ 'sth' , 'sth2' ] ))
185185
186186 result = df [[('foo' , 'bar' ), ('baz' , 'qux' )]]
187187 expected = df .ix [:, :2 ]
188188 assert_frame_equal (result , expected )
189- self .assertEqual (result .columns .name , 'sth' )
189+ self .assertEqual (result .columns .names , [ 'sth' , 'sth2' ] )
190190
191191 def test_setitem_list (self ):
192192
@@ -2499,6 +2499,31 @@ def test_constructor_dict_of_tuples(self):
24992499 expected = DataFrame (dict ((k , list (v )) for k , v in compat .iteritems (data )))
25002500 assert_frame_equal (result , expected , check_dtype = False )
25012501
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+
25022527 def _check_basic_constructor (self , empty ):
25032528 "mat: 2d matrix with shpae (3, 2) to input. empty - makes sized objects"
25042529 mat = empty ((2 , 3 ), dtype = float )
@@ -2922,8 +2947,8 @@ class CustomDict(dict):
29222947 def test_constructor_ragged (self ):
29232948 data = {'A' : randn (10 ),
29242949 '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 )
29272952
29282953 def test_constructor_scalar (self ):
29292954 idx = Index (lrange (3 ))
@@ -12105,7 +12130,8 @@ def test_index_namedtuple(self):
1210512130 IndexType = namedtuple ("IndexType" , ["a" , "b" ])
1210612131 idx1 = IndexType ("foo" , "bar" )
1210712132 idx2 = IndexType ("baz" , "bof" )
12108- index = Index ([idx1 , idx2 ], name = "composite_index" )
12133+ index = Index ([idx1 , idx2 ],
12134+ name = "composite_index" , tupleize_cols = False )
1210912135 df = DataFrame ([(1 , 2 ), (3 , 4 )], index = index , columns = ["A" , "B" ])
1211012136 self .assertEqual (df .ix [IndexType ("foo" , "bar" )]["A" ], 1 )
1211112137
0 commit comments