diff --git a/pandas/_libs/parsers.pyx b/pandas/_libs/parsers.pyx index 543a943aea311..f306916f9cfda 100644 --- a/pandas/_libs/parsers.pyx +++ b/pandas/_libs/parsers.pyx @@ -535,18 +535,25 @@ cdef class TextReader: self.parser_start = 0 self.header = [] else: - if isinstance(header, list) and len(header): - # need to artifically skip the final line - # which is still a header line - header = list(header) - header.append(header[-1] + 1) - - self.parser.header_start = header[0] - self.parser.header_end = header[-1] - self.parser.header = header[0] - self.parser_start = header[-1] + 1 - self.has_mi_columns = 1 - self.header = header + if isinstance(header, list): + if len(header) > 1: + # need to artifically skip the final line + # which is still a header line + header = list(header) + header.append(header[-1] + 1) + + self.parser.header_start = header[0] + self.parser.header_end = header[-1] + self.parser.header = header[0] + self.parser_start = header[-1] + 1 + self.has_mi_columns = 1 + self.header = header + else: + self.parser.header_start = header[0] + self.parser.header_end = header[0] + self.parser.header = header[0] + self.parser_start = header[0] + 1 + self.header = header else: self.parser.header_start = header self.parser.header_end = header diff --git a/pandas/io/parsers.py b/pandas/io/parsers.py index ea0bb104338b6..cda36c1e16046 100755 --- a/pandas/io/parsers.py +++ b/pandas/io/parsers.py @@ -2281,8 +2281,12 @@ def _infer_columns(self): # we have a mi columns, so read an extra line if isinstance(header, (list, tuple, np.ndarray)): - have_mi_columns = True - header = list(header) + [header[-1] + 1] + if len(header) > 1: + have_mi_columns = True + header = list(header) + [header[-1] + 1] + # Don't add second element to header if it's of length 1 + else: + have_mi_columns = False else: have_mi_columns = False header = [header] diff --git a/pandas/tests/io/parser/header.py b/pandas/tests/io/parser/header.py index 4935fd2cd910a..f91a4fb4d50fb 100644 --- a/pandas/tests/io/parser/header.py +++ b/pandas/tests/io/parser/header.py @@ -286,3 +286,10 @@ def test_non_int_header(self): self.read_csv(StringIO(data), sep=',', header=['a', 'b']) with tm.assert_raises_regex(ValueError, msg): self.read_csv(StringIO(data), sep=',', header='string_header') + + def test_singleton_header(self): + # See GH #7757 + data = """a,b,c\n0,1,2\n1,2,3""" + df = self.read_csv(StringIO(data), header=[0]) + expected = pd.DataFrame({'a': [0, 1], 'b': [1, 2], 'c': [2, 3]}) + tm.assert_frame_equal(df, expected)