diff --git a/pandas/io/parsers.py b/pandas/io/parsers.py index bd0649a7a85f3..bc7002c6b89b0 100644 --- a/pandas/io/parsers.py +++ b/pandas/io/parsers.py @@ -972,7 +972,7 @@ def __init__(self, src, **kwds): if self.names is None: if self.prefix: - self.names = ['X%d' % i + self.names = ['%s%d' % (self.prefix, i) for i in range(self._reader.table_width)] else: self.names = lrange(self._reader.table_width) @@ -1563,7 +1563,7 @@ def _infer_columns(self): num_original_columns = ncols if not names: if self.prefix: - columns = [['X%d' % i for i in range(ncols)]] + columns = [['%s%d' % (self.prefix, i) for i in range(ncols)]] else: columns = [lrange(ncols)] columns = self._handle_usecols(columns, columns[0]) diff --git a/pandas/io/tests/test_parsers.py b/pandas/io/tests/test_parsers.py index 93a26b70a019e..4f52f4c2fdbe4 100644 --- a/pandas/io/tests/test_parsers.py +++ b/pandas/io/tests/test_parsers.py @@ -2072,6 +2072,24 @@ def test_catch_too_many_names(self): 10,11,12\n""" tm.assertRaises(Exception, read_csv, StringIO(data), header=0, names=['a', 'b', 'c', 'd']) + def test_with_prefix(self): + # Issue 5732 + data = """\ +1,2,3 +4,,6 +7,8,9 +10,11,12\n""" + result = self.read_csv(StringIO(data), header=None, prefix="abc") + tm.assert_equal(list(result.columns), ['abc0', 'abc1', 'abc2']) + + # Test ignore prefix if not inferring headers. + result = self.read_csv(StringIO(data), prefix="abc") + tm.assert_equal(list(result.columns), ['1', '2', '3']) + + # Test ignore prefix if not inferring headers. + result = self.read_csv(StringIO(data), prefix="abc", names=['a', 'b', 'c']) + tm.assert_equal(list(result.columns), ['a', 'b', 'c']) + class TestPythonParser(ParserTests, tm.TestCase): def test_negative_skipfooter_raises(self):