@@ -1348,6 +1348,43 @@ def test_format_sparse_config(self):
1348
1348
1349
1349
warnings .filters = warn_filters
1350
1350
1351
+ def test_to_dataframe (self ):
1352
+ tuples = [(1 , 'one' ), (1 , 'two' ), (2 , 'one' ), (2 , 'two' )]
1353
+
1354
+ index = MultiIndex .from_tuples (tuples )
1355
+ result = index .to_dataframe (index = False )
1356
+ expected = DataFrame (tuples )
1357
+ tm .assert_frame_equal (result , expected )
1358
+
1359
+ result = index .to_dataframe ()
1360
+ expected .index = index
1361
+ tm .assert_frame_equal (result , expected )
1362
+
1363
+ tuples = [(1 , 'one' ), (1 , 'two' ), (2 , 'one' ), (2 , 'two' )]
1364
+ index = MultiIndex .from_tuples (tuples , names = ['first' , 'second' ])
1365
+ result = index .to_dataframe (index = False )
1366
+ expected = DataFrame (tuples )
1367
+ expected .columns = ['first' , 'second' ]
1368
+ tm .assert_frame_equal (result , expected )
1369
+
1370
+ result = index .to_dataframe ()
1371
+ expected .index = index
1372
+ tm .assert_frame_equal (result , expected )
1373
+
1374
+ index = MultiIndex .from_product ([range (5 ),
1375
+ pd .date_range ('20130101' , periods = 3 )])
1376
+ result = index .to_dataframe (index = False )
1377
+ expected = DataFrame (
1378
+ {0 : np .repeat (np .arange (5 , dtype = 'int64' ), 3 ),
1379
+ 1 : np .tile (pd .date_range ('20130101' , periods = 3 ), 5 )})
1380
+ tm .assert_frame_equal (result , expected )
1381
+
1382
+ index = MultiIndex .from_product ([range (5 ),
1383
+ pd .date_range ('20130101' , periods = 3 )])
1384
+ result = index .to_dataframe ()
1385
+ expected .index = index
1386
+ tm .assert_frame_equal (result , expected )
1387
+
1351
1388
def test_to_hierarchical (self ):
1352
1389
index = MultiIndex .from_tuples ([(1 , 'one' ), (1 , 'two' ), (2 , 'one' ), (
1353
1390
2 , 'two' )])
0 commit comments