Skip to content

Commit afc8360

Browse files
committed
TST: test for 6051 read_csv with multiindex columns
1 parent 37dfcc1 commit afc8360

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

pandas/tests/io/parser/test_header.py

+30-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from pandas.errors import ParserError
1313

14-
from pandas import DataFrame, Index, MultiIndex
14+
from pandas import DataFrame, Index, MultiIndex, read_csv
1515
import pandas.util.testing as tm
1616

1717

@@ -540,3 +540,32 @@ def test_multi_index_unnamed(all_parsers, index_col, columns):
540540
columns = MultiIndex.from_tuples(zip(exp_columns, ["0", "1"]))
541541
expected = DataFrame([[2, 3], [4, 5]], columns=columns)
542542
tm.assert_frame_equal(result, expected)
543+
544+
545+
def test_read_csv_multiindex_columns():
546+
# GH#6051
547+
s1 = "Male, Male, Male, Female, Female\nR, R, L, R, R\n.86, .67, .88, .78, .81"
548+
s2 = (
549+
"Male, Male, Male, Female, Female\n"
550+
"R, R, L, R, R\n"
551+
".86, .67, .88, .78, .81\n"
552+
".86, .67, .88, .78, .82"
553+
)
554+
555+
mi = MultiIndex.from_tuples(
556+
[
557+
("Male", "R"),
558+
(" Male", " R"),
559+
(" Male", " L"),
560+
(" Female", " R"),
561+
(" Female", " R.1"),
562+
]
563+
)
564+
expected = DataFrame(
565+
[[0.86, 0.67, 0.88, 0.78, 0.81], [0.86, 0.67, 0.88, 0.78, 0.82]], columns=mi
566+
)
567+
568+
df1 = read_csv(StringIO(s1), header=[0, 1])
569+
tm.assert_frame_equal(df1, expected.iloc[:1])
570+
df2 = read_csv(StringIO(s2), header=[0, 1])
571+
tm.assert_frame_equal(df2, expected)

0 commit comments

Comments
 (0)