Skip to content

Fix prefix argument for read_csv/read_table #5733

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
2 commits merged into from
Jan 3, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pandas/io/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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])
Expand Down
16 changes: 16 additions & 0 deletions pandas/io/tests/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,22 @@ def test_no_header(self):

self.assert_(np.array_equal(df2.columns, names))

def test_no_header_prefix(self):
data = """1,2,3,4,5
6,7,8,9,10
11,12,13,14,15
"""
df_pref = self.read_table(StringIO(data), sep=',', prefix='Field',
header=None)

expected = [[1, 2, 3, 4, 5.],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15]]
tm.assert_almost_equal(df_pref.values, expected)

self.assert_(np.array_equal(df_pref.columns,
['Field0', 'Field1', 'Field2', 'Field3', 'Field4']))

def test_header_with_index_col(self):
data = """foo,1,2,3
bar,4,5,6
Expand Down