@@ -1215,29 +1215,113 @@ def test_header_multi_index(self):
1215
1215
R_l0_g4,R_l1_g4,R4C0,R4C1,R4C2
1216
1216
"""
1217
1217
1218
- df = read_csv (StringIO (data ), header = [0 , 2 , 3 , 4 ], index_col = [0 , 1 ], tupleize_cols = False )
1218
+ df = self . read_csv (StringIO (data ), header = [0 , 2 , 3 , 4 ], index_col = [0 , 1 ], tupleize_cols = False )
1219
1219
tm .assert_frame_equal (df , expected )
1220
1220
1221
1221
# skipping lines in the header
1222
- df = read_csv (StringIO (data ), header = [0 , 2 , 3 , 4 ], index_col = [0 , 1 ], tupleize_cols = False )
1222
+ df = self . read_csv (StringIO (data ), header = [0 , 2 , 3 , 4 ], index_col = [0 , 1 ], tupleize_cols = False )
1223
1223
tm .assert_frame_equal (df , expected )
1224
1224
1225
1225
#### invalid options ####
1226
1226
1227
1227
# no as_recarray
1228
- self .assertRaises (ValueError , read_csv , StringIO (data ), header = [0 ,1 ,2 ,3 ],
1228
+ self .assertRaises (ValueError , self . read_csv , StringIO (data ), header = [0 ,1 ,2 ,3 ],
1229
1229
index_col = [0 ,1 ], as_recarray = True , tupleize_cols = False )
1230
1230
1231
1231
# names
1232
- self .assertRaises (ValueError , read_csv , StringIO (data ), header = [0 ,1 ,2 ,3 ],
1232
+ self .assertRaises (ValueError , self . read_csv , StringIO (data ), header = [0 ,1 ,2 ,3 ],
1233
1233
index_col = [0 ,1 ], names = ['foo' ,'bar' ], tupleize_cols = False )
1234
1234
# usecols
1235
- self .assertRaises (ValueError , read_csv , StringIO (data ), header = [0 ,1 ,2 ,3 ],
1235
+ self .assertRaises (ValueError , self . read_csv , StringIO (data ), header = [0 ,1 ,2 ,3 ],
1236
1236
index_col = [0 ,1 ], usecols = ['foo' ,'bar' ], tupleize_cols = False )
1237
1237
# non-numeric index_col
1238
- self .assertRaises (ValueError , read_csv , StringIO (data ), header = [0 ,1 ,2 ,3 ],
1238
+ self .assertRaises (ValueError , self . read_csv , StringIO (data ), header = [0 ,1 ,2 ,3 ],
1239
1239
index_col = ['foo' ,'bar' ], tupleize_cols = False )
1240
1240
1241
+ def test_header_multiindex_common_format (self ):
1242
+
1243
+ df = DataFrame ([[1 ,2 ,3 ,4 ,5 ,6 ],[7 ,8 ,9 ,10 ,11 ,12 ]],
1244
+ index = ['one' ,'two' ],
1245
+ columns = MultiIndex .from_tuples ([('a' ,'q' ),('a' ,'r' ),('a' ,'s' ),
1246
+ ('b' ,'t' ),('c' ,'u' ),('c' ,'v' )]))
1247
+
1248
+ # to_csv
1249
+ data = """,a,a,a,b,c,c
1250
+ ,q,r,s,t,u,v
1251
+ ,,,,,,
1252
+ one,1,2,3,4,5,6
1253
+ two,7,8,9,10,11,12"""
1254
+
1255
+ result = self .read_csv (StringIO (data ),header = [0 ,1 ],index_col = 0 )
1256
+ tm .assert_frame_equal (df ,result )
1257
+
1258
+ # common
1259
+ data = """,a,a,a,b,c,c
1260
+ ,q,r,s,t,u,v
1261
+ one,1,2,3,4,5,6
1262
+ two,7,8,9,10,11,12"""
1263
+
1264
+ result = self .read_csv (StringIO (data ),header = [0 ,1 ],index_col = 0 )
1265
+ tm .assert_frame_equal (df ,result )
1266
+
1267
+ # common, no index_col
1268
+ data = """a,a,a,b,c,c
1269
+ q,r,s,t,u,v
1270
+ 1,2,3,4,5,6
1271
+ 7,8,9,10,11,12"""
1272
+
1273
+ result = self .read_csv (StringIO (data ),header = [0 ,1 ],index_col = None )
1274
+ tm .assert_frame_equal (df .reset_index (drop = True ),result )
1275
+
1276
+ # malformed case 1
1277
+ expected = DataFrame (np .array ([[ 2 , 3 , 4 , 5 , 6 ],
1278
+ [ 8 , 9 , 10 , 11 , 12 ]]),
1279
+ index = Index ([1 , 7 ]),
1280
+ columns = MultiIndex (levels = [[u ('a' ), u ('b' ), u ('c' )], [u ('r' ), u ('s' ), u ('t' ), u ('u' ), u ('v' )]],
1281
+ labels = [[0 , 0 , 1 , 2 , 2 ], [0 , 1 , 2 , 3 , 4 ]],
1282
+ names = [u ('a' ), u ('q' )]))
1283
+
1284
+ data = """a,a,a,b,c,c
1285
+ q,r,s,t,u,v
1286
+ 1,2,3,4,5,6
1287
+ 7,8,9,10,11,12"""
1288
+
1289
+ result = self .read_csv (StringIO (data ),header = [0 ,1 ],index_col = 0 )
1290
+ tm .assert_frame_equal (expected ,result )
1291
+
1292
+ # malformed case 2
1293
+ expected = DataFrame (np .array ([[ 2 , 3 , 4 , 5 , 6 ],
1294
+ [ 8 , 9 , 10 , 11 , 12 ]]),
1295
+ index = Index ([1 , 7 ]),
1296
+ columns = MultiIndex (levels = [[u ('a' ), u ('b' ), u ('c' )], [u ('r' ), u ('s' ), u ('t' ), u ('u' ), u ('v' )]],
1297
+ labels = [[0 , 0 , 1 , 2 , 2 ], [0 , 1 , 2 , 3 , 4 ]],
1298
+ names = [None , u ('q' )]))
1299
+
1300
+ data = """,a,a,b,c,c
1301
+ q,r,s,t,u,v
1302
+ 1,2,3,4,5,6
1303
+ 7,8,9,10,11,12"""
1304
+
1305
+ result = self .read_csv (StringIO (data ),header = [0 ,1 ],index_col = 0 )
1306
+ tm .assert_frame_equal (expected ,result )
1307
+
1308
+ # mi on columns and index (malformed)
1309
+ expected = DataFrame (np .array ([[ 3 , 4 , 5 , 6 ],
1310
+ [ 9 , 10 , 11 , 12 ]]),
1311
+ index = MultiIndex (levels = [[1 , 7 ], [2 , 8 ]],
1312
+ labels = [[0 , 1 ], [0 , 1 ]]),
1313
+ columns = MultiIndex (levels = [[u ('a' ), u ('b' ), u ('c' )], [u ('s' ), u ('t' ), u ('u' ), u ('v' )]],
1314
+ labels = [[0 , 1 , 2 , 2 ], [0 , 1 , 2 , 3 ]],
1315
+ names = [None , u ('q' )]))
1316
+
1317
+ data = """,a,a,b,c,c
1318
+ q,r,s,t,u,v
1319
+ 1,2,3,4,5,6
1320
+ 7,8,9,10,11,12"""
1321
+
1322
+ result = self .read_csv (StringIO (data ),header = [0 ,1 ],index_col = [0 , 1 ])
1323
+ tm .assert_frame_equal (expected ,result )
1324
+
1241
1325
def test_pass_names_with_index (self ):
1242
1326
lines = self .data1 .split ('\n ' )
1243
1327
no_header = '\n ' .join (lines [1 :])
0 commit comments